irq-gic-v4.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016,2017 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/msi.h>
  10. #include <linux/sched.h>
  11. #include <linux/irqchip/arm-gic-v4.h>
  12. /*
  13. * WARNING: The blurb below assumes that you understand the
  14. * intricacies of GICv3, GICv4, and how a guest's view of a GICv3 gets
  15. * translated into GICv4 commands. So it effectively targets at most
  16. * two individuals. You know who you are.
  17. *
  18. * The core GICv4 code is designed to *avoid* exposing too much of the
  19. * core GIC code (that would in turn leak into the hypervisor code),
  20. * and instead provide a hypervisor agnostic interface to the HW (of
  21. * course, the astute reader will quickly realize that hypervisor
  22. * agnostic actually means KVM-specific - what were you thinking?).
  23. *
  24. * In order to achieve a modicum of isolation, we try to hide most of
  25. * the GICv4 "stuff" behind normal irqchip operations:
  26. *
  27. * - Any guest-visible VLPI is backed by a Linux interrupt (and a
  28. * physical LPI which gets unmapped when the guest maps the
  29. * VLPI). This allows the same DevID/EventID pair to be either
  30. * mapped to the LPI (host) or the VLPI (guest). Note that this is
  31. * exclusive, and you cannot have both.
  32. *
  33. * - Enabling/disabling a VLPI is done by issuing mask/unmask calls.
  34. *
  35. * - Guest INT/CLEAR commands are implemented through
  36. * irq_set_irqchip_state().
  37. *
  38. * - The *bizarre* stuff (mapping/unmapping an interrupt to a VLPI, or
  39. * issuing an INV after changing a priority) gets shoved into the
  40. * irq_set_vcpu_affinity() method. While this is quite horrible
  41. * (let's face it, this is the irqchip version of an ioctl), it
  42. * confines the crap to a single location. And map/unmap really is
  43. * about setting the affinity of a VLPI to a vcpu, so only INV is
  44. * majorly out of place. So there.
  45. *
  46. * A number of commands are simply not provided by this interface, as
  47. * they do not make direct sense. For example, MAPD is purely local to
  48. * the virtual ITS (because it references a virtual device, and the
  49. * physical ITS is still very much in charge of the physical
  50. * device). Same goes for things like MAPC (the physical ITS deals
  51. * with the actual vPE affinity, and not the braindead concept of
  52. * collection). SYNC is not provided either, as each and every command
  53. * is followed by a VSYNC. This could be relaxed in the future, should
  54. * this be seen as a bottleneck (yes, this means *never*).
  55. *
  56. * But handling VLPIs is only one side of the job of the GICv4
  57. * code. The other (darker) side is to take care of the doorbell
  58. * interrupts which are delivered when a VLPI targeting a non-running
  59. * vcpu is being made pending.
  60. *
  61. * The choice made here is that each vcpu (VPE in old northern GICv4
  62. * dialect) gets a single doorbell LPI, no matter how many interrupts
  63. * are targeting it. This has a nice property, which is that the
  64. * interrupt becomes a handle for the VPE, and that the hypervisor
  65. * code can manipulate it through the normal interrupt API:
  66. *
  67. * - VMs (or rather the VM abstraction that matters to the GIC)
  68. * contain an irq domain where each interrupt maps to a VPE. In
  69. * turn, this domain sits on top of the normal LPI allocator, and a
  70. * specially crafted irq_chip implementation.
  71. *
  72. * - mask/unmask do what is expected on the doorbell interrupt.
  73. *
  74. * - irq_set_affinity is used to move a VPE from one redistributor to
  75. * another.
  76. *
  77. * - irq_set_vcpu_affinity once again gets hijacked for the purpose of
  78. * creating a new sub-API, namely scheduling/descheduling a VPE
  79. * (which involves programming GICR_V{PROP,PEND}BASER) and
  80. * performing INVALL operations.
  81. */
  82. static struct irq_domain *gic_domain;
  83. static const struct irq_domain_ops *vpe_domain_ops;
  84. static const struct irq_domain_ops *sgi_domain_ops;
  85. #ifdef CONFIG_ARM64
  86. #include <asm/cpufeature.h>
  87. bool gic_cpuif_has_vsgi(void)
  88. {
  89. unsigned long fld, reg = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  90. fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64PFR0_EL1_GIC_SHIFT);
  91. return fld >= 0x3;
  92. }
  93. #else
  94. bool gic_cpuif_has_vsgi(void)
  95. {
  96. return false;
  97. }
  98. #endif
  99. static bool has_v4_1(void)
  100. {
  101. return !!sgi_domain_ops;
  102. }
  103. static bool has_v4_1_sgi(void)
  104. {
  105. return has_v4_1() && gic_cpuif_has_vsgi();
  106. }
  107. static int its_alloc_vcpu_sgis(struct its_vpe *vpe, int idx)
  108. {
  109. char *name;
  110. int sgi_base;
  111. if (!has_v4_1_sgi())
  112. return 0;
  113. name = kasprintf(GFP_KERNEL, "GICv4-sgi-%d", task_pid_nr(current));
  114. if (!name)
  115. goto err;
  116. vpe->fwnode = irq_domain_alloc_named_id_fwnode(name, idx);
  117. if (!vpe->fwnode)
  118. goto err;
  119. kfree(name);
  120. name = NULL;
  121. vpe->sgi_domain = irq_domain_create_linear(vpe->fwnode, 16,
  122. sgi_domain_ops, vpe);
  123. if (!vpe->sgi_domain)
  124. goto err;
  125. sgi_base = __irq_domain_alloc_irqs(vpe->sgi_domain, -1, 16,
  126. NUMA_NO_NODE, vpe,
  127. false, NULL);
  128. if (sgi_base <= 0)
  129. goto err;
  130. return 0;
  131. err:
  132. if (vpe->sgi_domain)
  133. irq_domain_remove(vpe->sgi_domain);
  134. if (vpe->fwnode)
  135. irq_domain_free_fwnode(vpe->fwnode);
  136. kfree(name);
  137. return -ENOMEM;
  138. }
  139. int its_alloc_vcpu_irqs(struct its_vm *vm)
  140. {
  141. int vpe_base_irq, i;
  142. vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv4-vpe",
  143. task_pid_nr(current));
  144. if (!vm->fwnode)
  145. goto err;
  146. vm->domain = irq_domain_create_hierarchy(gic_domain, 0, vm->nr_vpes,
  147. vm->fwnode, vpe_domain_ops,
  148. vm);
  149. if (!vm->domain)
  150. goto err;
  151. for (i = 0; i < vm->nr_vpes; i++) {
  152. vm->vpes[i]->its_vm = vm;
  153. vm->vpes[i]->idai = true;
  154. }
  155. vpe_base_irq = __irq_domain_alloc_irqs(vm->domain, -1, vm->nr_vpes,
  156. NUMA_NO_NODE, vm,
  157. false, NULL);
  158. if (vpe_base_irq <= 0)
  159. goto err;
  160. for (i = 0; i < vm->nr_vpes; i++) {
  161. int ret;
  162. vm->vpes[i]->irq = vpe_base_irq + i;
  163. ret = its_alloc_vcpu_sgis(vm->vpes[i], i);
  164. if (ret)
  165. goto err;
  166. }
  167. return 0;
  168. err:
  169. if (vm->domain)
  170. irq_domain_remove(vm->domain);
  171. if (vm->fwnode)
  172. irq_domain_free_fwnode(vm->fwnode);
  173. return -ENOMEM;
  174. }
  175. static void its_free_sgi_irqs(struct its_vm *vm)
  176. {
  177. int i;
  178. if (!has_v4_1_sgi())
  179. return;
  180. for (i = 0; i < vm->nr_vpes; i++) {
  181. unsigned int irq = irq_find_mapping(vm->vpes[i]->sgi_domain, 0);
  182. if (WARN_ON(!irq))
  183. continue;
  184. irq_domain_free_irqs(irq, 16);
  185. irq_domain_remove(vm->vpes[i]->sgi_domain);
  186. irq_domain_free_fwnode(vm->vpes[i]->fwnode);
  187. }
  188. }
  189. void its_free_vcpu_irqs(struct its_vm *vm)
  190. {
  191. its_free_sgi_irqs(vm);
  192. irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes);
  193. irq_domain_remove(vm->domain);
  194. irq_domain_free_fwnode(vm->fwnode);
  195. }
  196. static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info)
  197. {
  198. return irq_set_vcpu_affinity(vpe->irq, info);
  199. }
  200. int its_make_vpe_non_resident(struct its_vpe *vpe, bool db)
  201. {
  202. struct irq_desc *desc = irq_to_desc(vpe->irq);
  203. struct its_cmd_info info = { };
  204. int ret;
  205. WARN_ON(preemptible());
  206. info.cmd_type = DESCHEDULE_VPE;
  207. if (has_v4_1()) {
  208. /* GICv4.1 can directly deal with doorbells */
  209. info.req_db = db;
  210. } else {
  211. /* Undo the nested disable_irq() calls... */
  212. while (db && irqd_irq_disabled(&desc->irq_data))
  213. enable_irq(vpe->irq);
  214. }
  215. ret = its_send_vpe_cmd(vpe, &info);
  216. if (!ret)
  217. vpe->resident = false;
  218. vpe->ready = false;
  219. return ret;
  220. }
  221. int its_make_vpe_resident(struct its_vpe *vpe, bool g0en, bool g1en)
  222. {
  223. struct its_cmd_info info = { };
  224. int ret;
  225. WARN_ON(preemptible());
  226. info.cmd_type = SCHEDULE_VPE;
  227. if (has_v4_1()) {
  228. info.g0en = g0en;
  229. info.g1en = g1en;
  230. } else {
  231. /* Disabled the doorbell, as we're about to enter the guest */
  232. disable_irq_nosync(vpe->irq);
  233. }
  234. ret = its_send_vpe_cmd(vpe, &info);
  235. if (!ret)
  236. vpe->resident = true;
  237. return ret;
  238. }
  239. int its_commit_vpe(struct its_vpe *vpe)
  240. {
  241. struct its_cmd_info info = {
  242. .cmd_type = COMMIT_VPE,
  243. };
  244. int ret;
  245. WARN_ON(preemptible());
  246. ret = its_send_vpe_cmd(vpe, &info);
  247. if (!ret)
  248. vpe->ready = true;
  249. return ret;
  250. }
  251. int its_invall_vpe(struct its_vpe *vpe)
  252. {
  253. struct its_cmd_info info = {
  254. .cmd_type = INVALL_VPE,
  255. };
  256. return its_send_vpe_cmd(vpe, &info);
  257. }
  258. int its_map_vlpi(int irq, struct its_vlpi_map *map)
  259. {
  260. struct its_cmd_info info = {
  261. .cmd_type = MAP_VLPI,
  262. {
  263. .map = map,
  264. },
  265. };
  266. int ret;
  267. /*
  268. * The host will never see that interrupt firing again, so it
  269. * is vital that we don't do any lazy masking.
  270. */
  271. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  272. ret = irq_set_vcpu_affinity(irq, &info);
  273. if (ret)
  274. irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
  275. return ret;
  276. }
  277. int its_get_vlpi(int irq, struct its_vlpi_map *map)
  278. {
  279. struct its_cmd_info info = {
  280. .cmd_type = GET_VLPI,
  281. {
  282. .map = map,
  283. },
  284. };
  285. return irq_set_vcpu_affinity(irq, &info);
  286. }
  287. int its_unmap_vlpi(int irq)
  288. {
  289. irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
  290. return irq_set_vcpu_affinity(irq, NULL);
  291. }
  292. int its_prop_update_vlpi(int irq, u8 config, bool inv)
  293. {
  294. struct its_cmd_info info = {
  295. .cmd_type = inv ? PROP_UPDATE_AND_INV_VLPI : PROP_UPDATE_VLPI,
  296. {
  297. .config = config,
  298. },
  299. };
  300. return irq_set_vcpu_affinity(irq, &info);
  301. }
  302. int its_prop_update_vsgi(int irq, u8 priority, bool group)
  303. {
  304. struct its_cmd_info info = {
  305. .cmd_type = PROP_UPDATE_VSGI,
  306. {
  307. .priority = priority,
  308. .group = group,
  309. },
  310. };
  311. return irq_set_vcpu_affinity(irq, &info);
  312. }
  313. int its_init_v4(struct irq_domain *domain,
  314. const struct irq_domain_ops *vpe_ops,
  315. const struct irq_domain_ops *sgi_ops)
  316. {
  317. if (domain) {
  318. pr_info("ITS: Enabling GICv4 support\n");
  319. gic_domain = domain;
  320. vpe_domain_ops = vpe_ops;
  321. sgi_domain_ops = sgi_ops;
  322. return 0;
  323. }
  324. pr_err("ITS: No GICv4 VPE domain allocated\n");
  325. return -ENODEV;
  326. }