irq-gic-v4.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2016,2017 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/msi.h>
  21. #include <linux/sched.h>
  22. #include <linux/irqchip/arm-gic-v4.h>
  23. /*
  24. * WARNING: The blurb below assumes that you understand the
  25. * intricacies of GICv3, GICv4, and how a guest's view of a GICv3 gets
  26. * translated into GICv4 commands. So it effectively targets at most
  27. * two individuals. You know who you are.
  28. *
  29. * The core GICv4 code is designed to *avoid* exposing too much of the
  30. * core GIC code (that would in turn leak into the hypervisor code),
  31. * and instead provide a hypervisor agnostic interface to the HW (of
  32. * course, the astute reader will quickly realize that hypervisor
  33. * agnostic actually means KVM-specific - what were you thinking?).
  34. *
  35. * In order to achieve a modicum of isolation, we try to hide most of
  36. * the GICv4 "stuff" behind normal irqchip operations:
  37. *
  38. * - Any guest-visible VLPI is backed by a Linux interrupt (and a
  39. * physical LPI which gets unmapped when the guest maps the
  40. * VLPI). This allows the same DevID/EventID pair to be either
  41. * mapped to the LPI (host) or the VLPI (guest). Note that this is
  42. * exclusive, and you cannot have both.
  43. *
  44. * - Enabling/disabling a VLPI is done by issuing mask/unmask calls.
  45. *
  46. * - Guest INT/CLEAR commands are implemented through
  47. * irq_set_irqchip_state().
  48. *
  49. * - The *bizarre* stuff (mapping/unmapping an interrupt to a VLPI, or
  50. * issuing an INV after changing a priority) gets shoved into the
  51. * irq_set_vcpu_affinity() method. While this is quite horrible
  52. * (let's face it, this is the irqchip version of an ioctl), it
  53. * confines the crap to a single location. And map/unmap really is
  54. * about setting the affinity of a VLPI to a vcpu, so only INV is
  55. * majorly out of place. So there.
  56. *
  57. * A number of commands are simply not provided by this interface, as
  58. * they do not make direct sense. For example, MAPD is purely local to
  59. * the virtual ITS (because it references a virtual device, and the
  60. * physical ITS is still very much in charge of the physical
  61. * device). Same goes for things like MAPC (the physical ITS deals
  62. * with the actual vPE affinity, and not the braindead concept of
  63. * collection). SYNC is not provided either, as each and every command
  64. * is followed by a VSYNC. This could be relaxed in the future, should
  65. * this be seen as a bottleneck (yes, this means *never*).
  66. *
  67. * But handling VLPIs is only one side of the job of the GICv4
  68. * code. The other (darker) side is to take care of the doorbell
  69. * interrupts which are delivered when a VLPI targeting a non-running
  70. * vcpu is being made pending.
  71. *
  72. * The choice made here is that each vcpu (VPE in old northern GICv4
  73. * dialect) gets a single doorbell LPI, no matter how many interrupts
  74. * are targeting it. This has a nice property, which is that the
  75. * interrupt becomes a handle for the VPE, and that the hypervisor
  76. * code can manipulate it through the normal interrupt API:
  77. *
  78. * - VMs (or rather the VM abstraction that matters to the GIC)
  79. * contain an irq domain where each interrupt maps to a VPE. In
  80. * turn, this domain sits on top of the normal LPI allocator, and a
  81. * specially crafted irq_chip implementation.
  82. *
  83. * - mask/unmask do what is expected on the doorbell interrupt.
  84. *
  85. * - irq_set_affinity is used to move a VPE from one redistributor to
  86. * another.
  87. *
  88. * - irq_set_vcpu_affinity once again gets hijacked for the purpose of
  89. * creating a new sub-API, namely scheduling/descheduling a VPE
  90. * (which involves programming GICR_V{PROP,PEND}BASER) and
  91. * performing INVALL operations.
  92. */
  93. static struct irq_domain *gic_domain;
  94. static const struct irq_domain_ops *vpe_domain_ops;
  95. int its_alloc_vcpu_irqs(struct its_vm *vm)
  96. {
  97. int vpe_base_irq, i;
  98. vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv4-vpe",
  99. task_pid_nr(current));
  100. if (!vm->fwnode)
  101. goto err;
  102. vm->domain = irq_domain_create_hierarchy(gic_domain, 0, vm->nr_vpes,
  103. vm->fwnode, vpe_domain_ops,
  104. vm);
  105. if (!vm->domain)
  106. goto err;
  107. for (i = 0; i < vm->nr_vpes; i++) {
  108. vm->vpes[i]->its_vm = vm;
  109. vm->vpes[i]->idai = true;
  110. }
  111. vpe_base_irq = __irq_domain_alloc_irqs(vm->domain, -1, vm->nr_vpes,
  112. NUMA_NO_NODE, vm,
  113. false, NULL);
  114. if (vpe_base_irq <= 0)
  115. goto err;
  116. for (i = 0; i < vm->nr_vpes; i++)
  117. vm->vpes[i]->irq = vpe_base_irq + i;
  118. return 0;
  119. err:
  120. if (vm->domain)
  121. irq_domain_remove(vm->domain);
  122. if (vm->fwnode)
  123. irq_domain_free_fwnode(vm->fwnode);
  124. return -ENOMEM;
  125. }
  126. void its_free_vcpu_irqs(struct its_vm *vm)
  127. {
  128. irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes);
  129. irq_domain_remove(vm->domain);
  130. irq_domain_free_fwnode(vm->fwnode);
  131. }
  132. static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info)
  133. {
  134. return irq_set_vcpu_affinity(vpe->irq, info);
  135. }
  136. int its_schedule_vpe(struct its_vpe *vpe, bool on)
  137. {
  138. struct its_cmd_info info;
  139. WARN_ON(preemptible());
  140. info.cmd_type = on ? SCHEDULE_VPE : DESCHEDULE_VPE;
  141. return its_send_vpe_cmd(vpe, &info);
  142. }
  143. int its_invall_vpe(struct its_vpe *vpe)
  144. {
  145. struct its_cmd_info info = {
  146. .cmd_type = INVALL_VPE,
  147. };
  148. return its_send_vpe_cmd(vpe, &info);
  149. }
  150. int its_map_vlpi(int irq, struct its_vlpi_map *map)
  151. {
  152. struct its_cmd_info info = {
  153. .cmd_type = MAP_VLPI,
  154. {
  155. .map = map,
  156. },
  157. };
  158. int ret;
  159. /*
  160. * The host will never see that interrupt firing again, so it
  161. * is vital that we don't do any lazy masking.
  162. */
  163. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  164. ret = irq_set_vcpu_affinity(irq, &info);
  165. if (ret)
  166. irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
  167. return ret;
  168. }
  169. int its_get_vlpi(int irq, struct its_vlpi_map *map)
  170. {
  171. struct its_cmd_info info = {
  172. .cmd_type = GET_VLPI,
  173. {
  174. .map = map,
  175. },
  176. };
  177. return irq_set_vcpu_affinity(irq, &info);
  178. }
  179. int its_unmap_vlpi(int irq)
  180. {
  181. irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY);
  182. return irq_set_vcpu_affinity(irq, NULL);
  183. }
  184. int its_prop_update_vlpi(int irq, u8 config, bool inv)
  185. {
  186. struct its_cmd_info info = {
  187. .cmd_type = inv ? PROP_UPDATE_AND_INV_VLPI : PROP_UPDATE_VLPI,
  188. {
  189. .config = config,
  190. },
  191. };
  192. return irq_set_vcpu_affinity(irq, &info);
  193. }
  194. int its_init_v4(struct irq_domain *domain, const struct irq_domain_ops *ops)
  195. {
  196. if (domain) {
  197. pr_info("ITS: Enabling GICv4 support\n");
  198. gic_domain = domain;
  199. vpe_domain_ops = ops;
  200. return 0;
  201. }
  202. pr_err("ITS: No GICv4 VPE domain allocated\n");
  203. return -ENODEV;
  204. }