vgic-init.c 13 KB

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