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. return -EINVAL;
  169. }
  170. }
  171. return 0;
  172. }
  173. /**
  174. * kvm_vgic_vcpu_init() - Initialize static VGIC VCPU data
  175. * structures and register VCPU-specific KVM iodevs
  176. *
  177. * @vcpu: pointer to the VCPU being created and initialized
  178. *
  179. * Only do initialization, but do not actually enable the
  180. * VGIC CPU interface
  181. */
  182. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  183. {
  184. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  185. struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
  186. int ret = 0;
  187. int i;
  188. vgic_cpu->rd_iodev.base_addr = VGIC_ADDR_UNDEF;
  189. vgic_cpu->sgi_iodev.base_addr = VGIC_ADDR_UNDEF;
  190. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  191. spin_lock_init(&vgic_cpu->ap_list_lock);
  192. /*
  193. * Enable and configure all SGIs to be edge-triggered and
  194. * configure all PPIs as level-triggered.
  195. */
  196. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  197. struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
  198. INIT_LIST_HEAD(&irq->ap_list);
  199. spin_lock_init(&irq->irq_lock);
  200. irq->intid = i;
  201. irq->vcpu = NULL;
  202. irq->target_vcpu = vcpu;
  203. kref_init(&irq->refcount);
  204. if (vgic_irq_is_sgi(i)) {
  205. /* SGIs */
  206. irq->enabled = 1;
  207. irq->config = VGIC_CONFIG_EDGE;
  208. } else {
  209. /* PPIs */
  210. irq->config = VGIC_CONFIG_LEVEL;
  211. }
  212. }
  213. if (!irqchip_in_kernel(vcpu->kvm))
  214. return 0;
  215. /*
  216. * If we are creating a VCPU with a GICv3 we must also register the
  217. * KVM io device for the redistributor that belongs to this VCPU.
  218. */
  219. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  220. mutex_lock(&vcpu->kvm->lock);
  221. ret = vgic_register_redist_iodev(vcpu);
  222. mutex_unlock(&vcpu->kvm->lock);
  223. }
  224. return ret;
  225. }
  226. static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)
  227. {
  228. if (kvm_vgic_global_state.type == VGIC_V2)
  229. vgic_v2_enable(vcpu);
  230. else
  231. vgic_v3_enable(vcpu);
  232. }
  233. /*
  234. * vgic_init: allocates and initializes dist and vcpu data structures
  235. * depending on two dimensioning parameters:
  236. * - the number of spis
  237. * - the number of vcpus
  238. * The function is generally called when nr_spis has been explicitly set
  239. * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
  240. * vgic_initialized() returns true when this function has succeeded.
  241. * Must be called with kvm->lock held!
  242. */
  243. int vgic_init(struct kvm *kvm)
  244. {
  245. struct vgic_dist *dist = &kvm->arch.vgic;
  246. struct kvm_vcpu *vcpu;
  247. int ret = 0, i, idx;
  248. if (vgic_initialized(kvm))
  249. return 0;
  250. /* Are we also in the middle of creating a VCPU? */
  251. if (kvm->created_vcpus != atomic_read(&kvm->online_vcpus))
  252. return -EBUSY;
  253. /* freeze the number of spis */
  254. if (!dist->nr_spis)
  255. dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
  256. ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
  257. if (ret)
  258. goto out;
  259. /* Initialize groups on CPUs created before the VGIC type was known */
  260. kvm_for_each_vcpu(idx, vcpu, kvm) {
  261. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  262. for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++) {
  263. struct vgic_irq *irq = &vgic_cpu->private_irqs[i];
  264. switch (dist->vgic_model) {
  265. case KVM_DEV_TYPE_ARM_VGIC_V3:
  266. irq->group = 1;
  267. irq->mpidr = kvm_vcpu_get_mpidr_aff(vcpu);
  268. break;
  269. case KVM_DEV_TYPE_ARM_VGIC_V2:
  270. irq->group = 0;
  271. irq->targets = 1U << idx;
  272. break;
  273. default:
  274. ret = -EINVAL;
  275. goto out;
  276. }
  277. }
  278. }
  279. if (vgic_has_its(kvm)) {
  280. ret = vgic_v4_init(kvm);
  281. if (ret)
  282. goto out;
  283. }
  284. kvm_for_each_vcpu(i, vcpu, kvm)
  285. kvm_vgic_vcpu_enable(vcpu);
  286. ret = kvm_vgic_setup_default_irq_routing(kvm);
  287. if (ret)
  288. goto out;
  289. vgic_debug_init(kvm);
  290. dist->implementation_rev = 2;
  291. dist->initialized = true;
  292. out:
  293. return ret;
  294. }
  295. static void kvm_vgic_dist_destroy(struct kvm *kvm)
  296. {
  297. struct vgic_dist *dist = &kvm->arch.vgic;
  298. struct vgic_redist_region *rdreg, *next;
  299. dist->ready = false;
  300. dist->initialized = false;
  301. kfree(dist->spis);
  302. dist->spis = NULL;
  303. dist->nr_spis = 0;
  304. if (kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
  305. list_for_each_entry_safe(rdreg, next, &dist->rd_regions, list) {
  306. list_del(&rdreg->list);
  307. kfree(rdreg);
  308. }
  309. INIT_LIST_HEAD(&dist->rd_regions);
  310. }
  311. if (vgic_supports_direct_msis(kvm))
  312. vgic_v4_teardown(kvm);
  313. }
  314. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  315. {
  316. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  317. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  318. }
  319. /* To be called with kvm->lock held */
  320. static void __kvm_vgic_destroy(struct kvm *kvm)
  321. {
  322. struct kvm_vcpu *vcpu;
  323. int i;
  324. vgic_debug_destroy(kvm);
  325. kvm_vgic_dist_destroy(kvm);
  326. kvm_for_each_vcpu(i, vcpu, kvm)
  327. kvm_vgic_vcpu_destroy(vcpu);
  328. }
  329. void kvm_vgic_destroy(struct kvm *kvm)
  330. {
  331. mutex_lock(&kvm->lock);
  332. __kvm_vgic_destroy(kvm);
  333. mutex_unlock(&kvm->lock);
  334. }
  335. /**
  336. * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
  337. * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
  338. * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
  339. * @kvm: kvm struct pointer
  340. */
  341. int vgic_lazy_init(struct kvm *kvm)
  342. {
  343. int ret = 0;
  344. if (unlikely(!vgic_initialized(kvm))) {
  345. /*
  346. * We only provide the automatic initialization of the VGIC
  347. * for the legacy case of a GICv2. Any other type must
  348. * be explicitly initialized once setup with the respective
  349. * KVM device call.
  350. */
  351. if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
  352. return -EBUSY;
  353. mutex_lock(&kvm->lock);
  354. ret = vgic_init(kvm);
  355. mutex_unlock(&kvm->lock);
  356. }
  357. return ret;
  358. }
  359. /* RESOURCE MAPPING */
  360. /**
  361. * Map the MMIO regions depending on the VGIC model exposed to the guest
  362. * called on the first VCPU run.
  363. * Also map the virtual CPU interface into the VM.
  364. * v2/v3 derivatives call vgic_init if not already done.
  365. * vgic_ready() returns true if this function has succeeded.
  366. * @kvm: kvm struct pointer
  367. */
  368. int kvm_vgic_map_resources(struct kvm *kvm)
  369. {
  370. struct vgic_dist *dist = &kvm->arch.vgic;
  371. int ret = 0;
  372. mutex_lock(&kvm->lock);
  373. if (!irqchip_in_kernel(kvm))
  374. goto out;
  375. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
  376. ret = vgic_v2_map_resources(kvm);
  377. else
  378. ret = vgic_v3_map_resources(kvm);
  379. if (ret)
  380. __kvm_vgic_destroy(kvm);
  381. out:
  382. mutex_unlock(&kvm->lock);
  383. return ret;
  384. }
  385. /* GENERIC PROBE */
  386. static int vgic_init_cpu_starting(unsigned int cpu)
  387. {
  388. enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
  389. return 0;
  390. }
  391. static int vgic_init_cpu_dying(unsigned int cpu)
  392. {
  393. disable_percpu_irq(kvm_vgic_global_state.maint_irq);
  394. return 0;
  395. }
  396. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  397. {
  398. /*
  399. * We cannot rely on the vgic maintenance interrupt to be
  400. * delivered synchronously. This means we can only use it to
  401. * exit the VM, and we perform the handling of EOIed
  402. * interrupts on the exit path (see vgic_fold_lr_state).
  403. */
  404. return IRQ_HANDLED;
  405. }
  406. /**
  407. * kvm_vgic_init_cpu_hardware - initialize the GIC VE hardware
  408. *
  409. * For a specific CPU, initialize the GIC VE hardware.
  410. */
  411. void kvm_vgic_init_cpu_hardware(void)
  412. {
  413. BUG_ON(preemptible());
  414. /*
  415. * We want to make sure the list registers start out clear so that we
  416. * only have the program the used registers.
  417. */
  418. if (kvm_vgic_global_state.type == VGIC_V2)
  419. vgic_v2_init_lrs();
  420. else
  421. kvm_call_hyp(__vgic_v3_init_lrs);
  422. }
  423. /**
  424. * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  425. * according to the host GIC model. Accordingly calls either
  426. * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
  427. * instantiated by a guest later on .
  428. */
  429. int kvm_vgic_hyp_init(void)
  430. {
  431. const struct gic_kvm_info *gic_kvm_info;
  432. int ret;
  433. gic_kvm_info = gic_get_kvm_info();
  434. if (!gic_kvm_info)
  435. return -ENODEV;
  436. if (!gic_kvm_info->maint_irq) {
  437. kvm_err("No vgic maintenance irq\n");
  438. return -ENXIO;
  439. }
  440. switch (gic_kvm_info->type) {
  441. case GIC_V2:
  442. ret = vgic_v2_probe(gic_kvm_info);
  443. break;
  444. case GIC_V3:
  445. ret = vgic_v3_probe(gic_kvm_info);
  446. if (!ret) {
  447. static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
  448. kvm_info("GIC system register CPU interface enabled\n");
  449. }
  450. break;
  451. default:
  452. ret = -ENODEV;
  453. };
  454. if (ret)
  455. return ret;
  456. kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
  457. ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
  458. vgic_maintenance_handler,
  459. "vgic", kvm_get_running_vcpus());
  460. if (ret) {
  461. kvm_err("Cannot register interrupt %d\n",
  462. kvm_vgic_global_state.maint_irq);
  463. return ret;
  464. }
  465. ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
  466. "kvm/arm/vgic:starting",
  467. vgic_init_cpu_starting, vgic_init_cpu_dying);
  468. if (ret) {
  469. kvm_err("Cannot register vgic CPU notifier\n");
  470. goto out_free_irq;
  471. }
  472. kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
  473. return 0;
  474. out_free_irq:
  475. free_percpu_irq(kvm_vgic_global_state.maint_irq,
  476. kvm_get_running_vcpus());
  477. return ret;
  478. }