vgic-init.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.
  26. *
  27. * Distributor:
  28. *
  29. * - kvm_vgic_early_init(): initialization of static data that doesn't
  30. * depend on any sizing information or emulation type. No allocation
  31. * is allowed there.
  32. *
  33. * - vgic_init(): allocation and initialization of the generic data
  34. * structures that depend on sizing information (number of CPUs,
  35. * number of interrupts). Also initializes the vcpu specific data
  36. * structures. Can be executed lazily for GICv2.
  37. *
  38. * CPU Interface:
  39. *
  40. * - kvm_vgic_cpu_early_init(): initialization of static data that
  41. * doesn't depend on any sizing information or emulation type. No
  42. * allocation is allowed there.
  43. */
  44. /* EARLY INIT */
  45. /*
  46. * Those 2 functions should not be needed anymore but they
  47. * still are called from arm.c
  48. */
  49. void kvm_vgic_early_init(struct kvm *kvm)
  50. {
  51. }
  52. void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu)
  53. {
  54. }
  55. /* CREATION */
  56. /**
  57. * kvm_vgic_create: triggered by the instantiation of the VGIC device by
  58. * user space, either through the legacy KVM_CREATE_IRQCHIP ioctl (v2 only)
  59. * or through the generic KVM_CREATE_DEVICE API ioctl.
  60. * irqchip_in_kernel() tells you if this function succeeded or not.
  61. * @kvm: kvm struct pointer
  62. * @type: KVM_DEV_TYPE_ARM_VGIC_V[23]
  63. */
  64. int kvm_vgic_create(struct kvm *kvm, u32 type)
  65. {
  66. int i, vcpu_lock_idx = -1, ret;
  67. struct kvm_vcpu *vcpu;
  68. if (irqchip_in_kernel(kvm))
  69. return -EEXIST;
  70. /*
  71. * This function is also called by the KVM_CREATE_IRQCHIP handler,
  72. * which had no chance yet to check the availability of the GICv2
  73. * emulation. So check this here again. KVM_CREATE_DEVICE does
  74. * the proper checks already.
  75. */
  76. if (type == KVM_DEV_TYPE_ARM_VGIC_V2 &&
  77. !kvm_vgic_global_state.can_emulate_gicv2)
  78. return -ENODEV;
  79. /*
  80. * Any time a vcpu is run, vcpu_load is called which tries to grab the
  81. * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure
  82. * that no other VCPUs are run while we create the vgic.
  83. */
  84. ret = -EBUSY;
  85. kvm_for_each_vcpu(i, vcpu, kvm) {
  86. if (!mutex_trylock(&vcpu->mutex))
  87. goto out_unlock;
  88. vcpu_lock_idx = i;
  89. }
  90. kvm_for_each_vcpu(i, vcpu, kvm) {
  91. if (vcpu->arch.has_run_once)
  92. goto out_unlock;
  93. }
  94. ret = 0;
  95. if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
  96. kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
  97. else
  98. kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
  99. if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
  100. ret = -E2BIG;
  101. goto out_unlock;
  102. }
  103. kvm->arch.vgic.in_kernel = true;
  104. kvm->arch.vgic.vgic_model = type;
  105. /*
  106. * kvm_vgic_global_state.vctrl_base is set on vgic probe (kvm_arch_init)
  107. * it is stored in distributor struct for asm save/restore purpose
  108. */
  109. kvm->arch.vgic.vctrl_base = kvm_vgic_global_state.vctrl_base;
  110. kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
  111. kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
  112. kvm->arch.vgic.vgic_redist_base = VGIC_ADDR_UNDEF;
  113. out_unlock:
  114. for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
  115. vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
  116. mutex_unlock(&vcpu->mutex);
  117. }
  118. return ret;
  119. }
  120. /* INIT/DESTROY */
  121. /**
  122. * kvm_vgic_dist_init: initialize the dist data structures
  123. * @kvm: kvm struct pointer
  124. * @nr_spis: number of spis, frozen by caller
  125. */
  126. static int kvm_vgic_dist_init(struct kvm *kvm, unsigned int nr_spis)
  127. {
  128. struct vgic_dist *dist = &kvm->arch.vgic;
  129. struct kvm_vcpu *vcpu0 = kvm_get_vcpu(kvm, 0);
  130. int i;
  131. INIT_LIST_HEAD(&dist->lpi_list_head);
  132. spin_lock_init(&dist->lpi_list_lock);
  133. dist->spis = kcalloc(nr_spis, sizeof(struct vgic_irq), GFP_KERNEL);
  134. if (!dist->spis)
  135. return -ENOMEM;
  136. /*
  137. * In the following code we do not take the irq struct lock since
  138. * no other action on irq structs can happen while the VGIC is
  139. * not initialized yet:
  140. * If someone wants to inject an interrupt or does a MMIO access, we
  141. * require prior initialization in case of a virtual GICv3 or trigger
  142. * initialization when using a virtual GICv2.
  143. */
  144. for (i = 0; i < nr_spis; i++) {
  145. struct vgic_irq *irq = &dist->spis[i];
  146. irq->intid = i + VGIC_NR_PRIVATE_IRQS;
  147. INIT_LIST_HEAD(&irq->ap_list);
  148. spin_lock_init(&irq->irq_lock);
  149. irq->vcpu = NULL;
  150. irq->target_vcpu = vcpu0;
  151. kref_init(&irq->refcount);
  152. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
  153. irq->targets = 0;
  154. else
  155. irq->mpidr = 0;
  156. }
  157. return 0;
  158. }
  159. /**
  160. * kvm_vgic_vcpu_init: initialize the vcpu data structures and
  161. * enable the VCPU interface
  162. * @vcpu: the VCPU which's VGIC should be initialized
  163. */
  164. static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  165. {
  166. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  167. int i;
  168. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  169. spin_lock_init(&vgic_cpu->ap_list_lock);
  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. spin_lock_init(&irq->irq_lock);
  178. irq->intid = i;
  179. irq->vcpu = NULL;
  180. irq->target_vcpu = vcpu;
  181. irq->targets = 1U << vcpu->vcpu_id;
  182. kref_init(&irq->refcount);
  183. if (vgic_irq_is_sgi(i)) {
  184. /* SGIs */
  185. irq->enabled = 1;
  186. irq->config = VGIC_CONFIG_EDGE;
  187. } else {
  188. /* PPIs */
  189. irq->config = VGIC_CONFIG_LEVEL;
  190. }
  191. }
  192. if (kvm_vgic_global_state.type == VGIC_V2)
  193. vgic_v2_enable(vcpu);
  194. else
  195. vgic_v3_enable(vcpu);
  196. }
  197. /*
  198. * vgic_init: allocates and initializes dist and vcpu data structures
  199. * depending on two dimensioning parameters:
  200. * - the number of spis
  201. * - the number of vcpus
  202. * The function is generally called when nr_spis has been explicitly set
  203. * by the guest through the KVM DEVICE API. If not nr_spis is set to 256.
  204. * vgic_initialized() returns true when this function has succeeded.
  205. * Must be called with kvm->lock held!
  206. */
  207. int vgic_init(struct kvm *kvm)
  208. {
  209. struct vgic_dist *dist = &kvm->arch.vgic;
  210. struct kvm_vcpu *vcpu;
  211. int ret = 0, i;
  212. if (vgic_initialized(kvm))
  213. return 0;
  214. /* freeze the number of spis */
  215. if (!dist->nr_spis)
  216. dist->nr_spis = VGIC_NR_IRQS_LEGACY - VGIC_NR_PRIVATE_IRQS;
  217. ret = kvm_vgic_dist_init(kvm, dist->nr_spis);
  218. if (ret)
  219. goto out;
  220. if (vgic_has_its(kvm))
  221. dist->msis_require_devid = true;
  222. kvm_for_each_vcpu(i, vcpu, kvm)
  223. kvm_vgic_vcpu_init(vcpu);
  224. ret = kvm_vgic_setup_default_irq_routing(kvm);
  225. if (ret)
  226. goto out;
  227. dist->initialized = true;
  228. out:
  229. return ret;
  230. }
  231. static void kvm_vgic_dist_destroy(struct kvm *kvm)
  232. {
  233. struct vgic_dist *dist = &kvm->arch.vgic;
  234. dist->ready = false;
  235. dist->initialized = false;
  236. kfree(dist->spis);
  237. dist->nr_spis = 0;
  238. }
  239. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  240. {
  241. struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
  242. INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
  243. }
  244. /* To be called with kvm->lock held */
  245. static void __kvm_vgic_destroy(struct kvm *kvm)
  246. {
  247. struct kvm_vcpu *vcpu;
  248. int i;
  249. kvm_vgic_dist_destroy(kvm);
  250. kvm_for_each_vcpu(i, vcpu, kvm)
  251. kvm_vgic_vcpu_destroy(vcpu);
  252. }
  253. void kvm_vgic_destroy(struct kvm *kvm)
  254. {
  255. mutex_lock(&kvm->lock);
  256. __kvm_vgic_destroy(kvm);
  257. mutex_unlock(&kvm->lock);
  258. }
  259. /**
  260. * vgic_lazy_init: Lazy init is only allowed if the GIC exposed to the guest
  261. * is a GICv2. A GICv3 must be explicitly initialized by the guest using the
  262. * KVM_DEV_ARM_VGIC_GRP_CTRL KVM_DEVICE group.
  263. * @kvm: kvm struct pointer
  264. */
  265. int vgic_lazy_init(struct kvm *kvm)
  266. {
  267. int ret = 0;
  268. if (unlikely(!vgic_initialized(kvm))) {
  269. /*
  270. * We only provide the automatic initialization of the VGIC
  271. * for the legacy case of a GICv2. Any other type must
  272. * be explicitly initialized once setup with the respective
  273. * KVM device call.
  274. */
  275. if (kvm->arch.vgic.vgic_model != KVM_DEV_TYPE_ARM_VGIC_V2)
  276. return -EBUSY;
  277. mutex_lock(&kvm->lock);
  278. ret = vgic_init(kvm);
  279. mutex_unlock(&kvm->lock);
  280. }
  281. return ret;
  282. }
  283. /* RESOURCE MAPPING */
  284. /**
  285. * Map the MMIO regions depending on the VGIC model exposed to the guest
  286. * called on the first VCPU run.
  287. * Also map the virtual CPU interface into the VM.
  288. * v2/v3 derivatives call vgic_init if not already done.
  289. * vgic_ready() returns true if this function has succeeded.
  290. * @kvm: kvm struct pointer
  291. */
  292. int kvm_vgic_map_resources(struct kvm *kvm)
  293. {
  294. struct vgic_dist *dist = &kvm->arch.vgic;
  295. int ret = 0;
  296. mutex_lock(&kvm->lock);
  297. if (!irqchip_in_kernel(kvm))
  298. goto out;
  299. if (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V2)
  300. ret = vgic_v2_map_resources(kvm);
  301. else
  302. ret = vgic_v3_map_resources(kvm);
  303. if (ret)
  304. __kvm_vgic_destroy(kvm);
  305. out:
  306. mutex_unlock(&kvm->lock);
  307. return ret;
  308. }
  309. /* GENERIC PROBE */
  310. static int vgic_init_cpu_starting(unsigned int cpu)
  311. {
  312. enable_percpu_irq(kvm_vgic_global_state.maint_irq, 0);
  313. return 0;
  314. }
  315. static int vgic_init_cpu_dying(unsigned int cpu)
  316. {
  317. disable_percpu_irq(kvm_vgic_global_state.maint_irq);
  318. return 0;
  319. }
  320. static irqreturn_t vgic_maintenance_handler(int irq, void *data)
  321. {
  322. /*
  323. * We cannot rely on the vgic maintenance interrupt to be
  324. * delivered synchronously. This means we can only use it to
  325. * exit the VM, and we perform the handling of EOIed
  326. * interrupts on the exit path (see vgic_process_maintenance).
  327. */
  328. return IRQ_HANDLED;
  329. }
  330. /**
  331. * kvm_vgic_hyp_init: populates the kvm_vgic_global_state variable
  332. * according to the host GIC model. Accordingly calls either
  333. * vgic_v2/v3_probe which registers the KVM_DEVICE that can be
  334. * instantiated by a guest later on .
  335. */
  336. int kvm_vgic_hyp_init(void)
  337. {
  338. const struct gic_kvm_info *gic_kvm_info;
  339. int ret;
  340. gic_kvm_info = gic_get_kvm_info();
  341. if (!gic_kvm_info)
  342. return -ENODEV;
  343. if (!gic_kvm_info->maint_irq) {
  344. kvm_err("No vgic maintenance irq\n");
  345. return -ENXIO;
  346. }
  347. switch (gic_kvm_info->type) {
  348. case GIC_V2:
  349. ret = vgic_v2_probe(gic_kvm_info);
  350. break;
  351. case GIC_V3:
  352. ret = vgic_v3_probe(gic_kvm_info);
  353. if (!ret) {
  354. static_branch_enable(&kvm_vgic_global_state.gicv3_cpuif);
  355. kvm_info("GIC system register CPU interface enabled\n");
  356. }
  357. break;
  358. default:
  359. ret = -ENODEV;
  360. };
  361. if (ret)
  362. return ret;
  363. kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq;
  364. ret = request_percpu_irq(kvm_vgic_global_state.maint_irq,
  365. vgic_maintenance_handler,
  366. "vgic", kvm_get_running_vcpus());
  367. if (ret) {
  368. kvm_err("Cannot register interrupt %d\n",
  369. kvm_vgic_global_state.maint_irq);
  370. return ret;
  371. }
  372. ret = cpuhp_setup_state(CPUHP_AP_KVM_ARM_VGIC_INIT_STARTING,
  373. "AP_KVM_ARM_VGIC_INIT_STARTING",
  374. vgic_init_cpu_starting, vgic_init_cpu_dying);
  375. if (ret) {
  376. kvm_err("Cannot register vgic CPU notifier\n");
  377. goto out_free_irq;
  378. }
  379. kvm_info("vgic interrupt IRQ%d\n", kvm_vgic_global_state.maint_irq);
  380. return 0;
  381. out_free_irq:
  382. free_percpu_irq(kvm_vgic_global_state.maint_irq,
  383. kvm_get_running_vcpus());
  384. return ret;
  385. }