vgic-init.c 14 KB

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