arm_vgic.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #ifndef __KVM_ARM_VGIC_H
  17. #define __KVM_ARM_VGIC_H
  18. #include <linux/kernel.h>
  19. #include <linux/kvm.h>
  20. #include <linux/irqreturn.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <kvm/iodev.h>
  24. #include <linux/list.h>
  25. #define VGIC_V3_MAX_CPUS 255
  26. #define VGIC_V2_MAX_CPUS 8
  27. #define VGIC_NR_IRQS_LEGACY 256
  28. #define VGIC_NR_SGIS 16
  29. #define VGIC_NR_PPIS 16
  30. #define VGIC_NR_PRIVATE_IRQS (VGIC_NR_SGIS + VGIC_NR_PPIS)
  31. #define VGIC_MAX_PRIVATE (VGIC_NR_PRIVATE_IRQS - 1)
  32. #define VGIC_MAX_SPI 1019
  33. #define VGIC_MAX_RESERVED 1023
  34. #define VGIC_MIN_LPI 8192
  35. #define KVM_IRQCHIP_NUM_PINS (1020 - 32)
  36. enum vgic_type {
  37. VGIC_V2, /* Good ol' GICv2 */
  38. VGIC_V3, /* New fancy GICv3 */
  39. };
  40. /* same for all guests, as depending only on the _host's_ GIC model */
  41. struct vgic_global {
  42. /* type of the host GIC */
  43. enum vgic_type type;
  44. /* Physical address of vgic virtual cpu interface */
  45. phys_addr_t vcpu_base;
  46. /* virtual control interface mapping */
  47. void __iomem *vctrl_base;
  48. /* Number of implemented list registers */
  49. int nr_lr;
  50. /* Maintenance IRQ number */
  51. unsigned int maint_irq;
  52. /* maximum number of VCPUs allowed (GICv2 limits us to 8) */
  53. int max_gic_vcpus;
  54. /* Only needed for the legacy KVM_CREATE_IRQCHIP */
  55. bool can_emulate_gicv2;
  56. };
  57. extern struct vgic_global kvm_vgic_global_state;
  58. #define VGIC_V2_MAX_LRS (1 << 6)
  59. #define VGIC_V3_MAX_LRS 16
  60. #define VGIC_V3_LR_INDEX(lr) (VGIC_V3_MAX_LRS - 1 - lr)
  61. enum vgic_irq_config {
  62. VGIC_CONFIG_EDGE = 0,
  63. VGIC_CONFIG_LEVEL
  64. };
  65. struct vgic_irq {
  66. spinlock_t irq_lock; /* Protects the content of the struct */
  67. struct list_head lpi_list; /* Used to link all LPIs together */
  68. struct list_head ap_list;
  69. struct kvm_vcpu *vcpu; /* SGIs and PPIs: The VCPU
  70. * SPIs and LPIs: The VCPU whose ap_list
  71. * this is queued on.
  72. */
  73. struct kvm_vcpu *target_vcpu; /* The VCPU that this interrupt should
  74. * be sent to, as a result of the
  75. * targets reg (v2) or the
  76. * affinity reg (v3).
  77. */
  78. u32 intid; /* Guest visible INTID */
  79. bool pending;
  80. bool line_level; /* Level only */
  81. bool soft_pending; /* Level only */
  82. bool active; /* not used for LPIs */
  83. bool enabled;
  84. bool hw; /* Tied to HW IRQ */
  85. struct kref refcount; /* Used for LPIs */
  86. u32 hwintid; /* HW INTID number */
  87. union {
  88. u8 targets; /* GICv2 target VCPUs mask */
  89. u32 mpidr; /* GICv3 target VCPU */
  90. };
  91. u8 source; /* GICv2 SGIs only */
  92. u8 priority;
  93. enum vgic_irq_config config; /* Level or edge */
  94. };
  95. struct vgic_register_region;
  96. struct vgic_its;
  97. enum iodev_type {
  98. IODEV_CPUIF,
  99. IODEV_DIST,
  100. IODEV_REDIST,
  101. IODEV_ITS
  102. };
  103. struct vgic_io_device {
  104. gpa_t base_addr;
  105. union {
  106. struct kvm_vcpu *redist_vcpu;
  107. struct vgic_its *its;
  108. };
  109. const struct vgic_register_region *regions;
  110. enum iodev_type iodev_type;
  111. int nr_regions;
  112. struct kvm_io_device dev;
  113. };
  114. struct vgic_its {
  115. /* The base address of the ITS control register frame */
  116. gpa_t vgic_its_base;
  117. bool enabled;
  118. bool initialized;
  119. struct vgic_io_device iodev;
  120. struct kvm_device *dev;
  121. /* These registers correspond to GITS_BASER{0,1} */
  122. u64 baser_device_table;
  123. u64 baser_coll_table;
  124. /* Protects the command queue */
  125. struct mutex cmd_lock;
  126. u64 cbaser;
  127. u32 creadr;
  128. u32 cwriter;
  129. /* Protects the device and collection lists */
  130. struct mutex its_lock;
  131. struct list_head device_list;
  132. struct list_head collection_list;
  133. };
  134. struct vgic_dist {
  135. bool in_kernel;
  136. bool ready;
  137. bool initialized;
  138. /* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */
  139. u32 vgic_model;
  140. /* Do injected MSIs require an additional device ID? */
  141. bool msis_require_devid;
  142. int nr_spis;
  143. /* TODO: Consider moving to global state */
  144. /* Virtual control interface mapping */
  145. void __iomem *vctrl_base;
  146. /* base addresses in guest physical address space: */
  147. gpa_t vgic_dist_base; /* distributor */
  148. union {
  149. /* either a GICv2 CPU interface */
  150. gpa_t vgic_cpu_base;
  151. /* or a number of GICv3 redistributor regions */
  152. gpa_t vgic_redist_base;
  153. };
  154. /* distributor enabled */
  155. bool enabled;
  156. struct vgic_irq *spis;
  157. struct vgic_io_device dist_iodev;
  158. bool has_its;
  159. /*
  160. * Contains the attributes and gpa of the LPI configuration table.
  161. * Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share
  162. * one address across all redistributors.
  163. * GICv3 spec: 6.1.2 "LPI Configuration tables"
  164. */
  165. u64 propbaser;
  166. /* Protects the lpi_list and the count value below. */
  167. spinlock_t lpi_list_lock;
  168. struct list_head lpi_list_head;
  169. int lpi_list_count;
  170. };
  171. struct vgic_v2_cpu_if {
  172. u32 vgic_hcr;
  173. u32 vgic_vmcr;
  174. u32 vgic_misr; /* Saved only */
  175. u64 vgic_eisr; /* Saved only */
  176. u64 vgic_elrsr; /* Saved only */
  177. u32 vgic_apr;
  178. u32 vgic_lr[VGIC_V2_MAX_LRS];
  179. };
  180. struct vgic_v3_cpu_if {
  181. #ifdef CONFIG_KVM_ARM_VGIC_V3
  182. u32 vgic_hcr;
  183. u32 vgic_vmcr;
  184. u32 vgic_sre; /* Restored only, change ignored */
  185. u32 vgic_misr; /* Saved only */
  186. u32 vgic_eisr; /* Saved only */
  187. u32 vgic_elrsr; /* Saved only */
  188. u32 vgic_ap0r[4];
  189. u32 vgic_ap1r[4];
  190. u64 vgic_lr[VGIC_V3_MAX_LRS];
  191. #endif
  192. };
  193. struct vgic_cpu {
  194. /* CPU vif control registers for world switch */
  195. union {
  196. struct vgic_v2_cpu_if vgic_v2;
  197. struct vgic_v3_cpu_if vgic_v3;
  198. };
  199. unsigned int used_lrs;
  200. struct vgic_irq private_irqs[VGIC_NR_PRIVATE_IRQS];
  201. spinlock_t ap_list_lock; /* Protects the ap_list */
  202. /*
  203. * List of IRQs that this VCPU should consider because they are either
  204. * Active or Pending (hence the name; AP list), or because they recently
  205. * were one of the two and need to be migrated off this list to another
  206. * VCPU.
  207. */
  208. struct list_head ap_list_head;
  209. u64 live_lrs;
  210. /*
  211. * Members below are used with GICv3 emulation only and represent
  212. * parts of the redistributor.
  213. */
  214. struct vgic_io_device rd_iodev;
  215. struct vgic_io_device sgi_iodev;
  216. /* Contains the attributes and gpa of the LPI pending tables. */
  217. u64 pendbaser;
  218. bool lpis_enabled;
  219. };
  220. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
  221. void kvm_vgic_early_init(struct kvm *kvm);
  222. int kvm_vgic_create(struct kvm *kvm, u32 type);
  223. void kvm_vgic_destroy(struct kvm *kvm);
  224. void kvm_vgic_vcpu_early_init(struct kvm_vcpu *vcpu);
  225. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
  226. int kvm_vgic_map_resources(struct kvm *kvm);
  227. int kvm_vgic_hyp_init(void);
  228. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  229. bool level);
  230. int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid,
  231. bool level);
  232. int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq);
  233. int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq);
  234. bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq);
  235. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  236. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.in_kernel))
  237. #define vgic_initialized(k) ((k)->arch.vgic.initialized)
  238. #define vgic_ready(k) ((k)->arch.vgic.ready)
  239. #define vgic_valid_spi(k, i) (((i) >= VGIC_NR_PRIVATE_IRQS) && \
  240. ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
  241. bool kvm_vcpu_has_pending_irqs(struct kvm_vcpu *vcpu);
  242. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  243. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  244. #ifdef CONFIG_KVM_ARM_VGIC_V3
  245. void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg);
  246. #else
  247. static inline void vgic_v3_dispatch_sgi(struct kvm_vcpu *vcpu, u64 reg)
  248. {
  249. }
  250. #endif
  251. /**
  252. * kvm_vgic_get_max_vcpus - Get the maximum number of VCPUs allowed by HW
  253. *
  254. * The host's GIC naturally limits the maximum amount of VCPUs a guest
  255. * can use.
  256. */
  257. static inline int kvm_vgic_get_max_vcpus(void)
  258. {
  259. return kvm_vgic_global_state.max_gic_vcpus;
  260. }
  261. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi);
  262. /**
  263. * kvm_vgic_setup_default_irq_routing:
  264. * Setup a default flat gsi routing table mapping all SPIs
  265. */
  266. int kvm_vgic_setup_default_irq_routing(struct kvm *kvm);
  267. #endif /* __KVM_ARM_VGIC_H */