arm_vgic.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __ASM_ARM_KVM_VGIC_H
  19. #define __ASM_ARM_KVM_VGIC_H
  20. #include <linux/kernel.h>
  21. #include <linux/kvm.h>
  22. #include <linux/irqreturn.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/types.h>
  25. #define VGIC_NR_IRQS_LEGACY 256
  26. #define VGIC_NR_SGIS 16
  27. #define VGIC_NR_PPIS 16
  28. #define VGIC_NR_PRIVATE_IRQS (VGIC_NR_SGIS + VGIC_NR_PPIS)
  29. #define VGIC_V2_MAX_LRS (1 << 6)
  30. #define VGIC_V3_MAX_LRS 16
  31. #define VGIC_MAX_IRQS 1024
  32. /* Sanity checks... */
  33. #if (KVM_MAX_VCPUS > 8)
  34. #error Invalid number of CPU interfaces
  35. #endif
  36. #if (VGIC_NR_IRQS_LEGACY & 31)
  37. #error "VGIC_NR_IRQS must be a multiple of 32"
  38. #endif
  39. #if (VGIC_NR_IRQS_LEGACY > VGIC_MAX_IRQS)
  40. #error "VGIC_NR_IRQS must be <= 1024"
  41. #endif
  42. /*
  43. * The GIC distributor registers describing interrupts have two parts:
  44. * - 32 per-CPU interrupts (SGI + PPI)
  45. * - a bunch of shared interrupts (SPI)
  46. */
  47. struct vgic_bitmap {
  48. /*
  49. * - One UL per VCPU for private interrupts (assumes UL is at
  50. * least 32 bits)
  51. * - As many UL as necessary for shared interrupts.
  52. *
  53. * The private interrupts are accessed via the "private"
  54. * field, one UL per vcpu (the state for vcpu n is in
  55. * private[n]). The shared interrupts are accessed via the
  56. * "shared" pointer (IRQn state is at bit n-32 in the bitmap).
  57. */
  58. unsigned long *private;
  59. unsigned long *shared;
  60. };
  61. struct vgic_bytemap {
  62. /*
  63. * - 8 u32 per VCPU for private interrupts
  64. * - As many u32 as necessary for shared interrupts.
  65. *
  66. * The private interrupts are accessed via the "private"
  67. * field, (the state for vcpu n is in private[n*8] to
  68. * private[n*8 + 7]). The shared interrupts are accessed via
  69. * the "shared" pointer (IRQn state is at byte (n-32)%4 of the
  70. * shared[(n-32)/4] word).
  71. */
  72. u32 *private;
  73. u32 *shared;
  74. };
  75. struct kvm_vcpu;
  76. enum vgic_type {
  77. VGIC_V2, /* Good ol' GICv2 */
  78. VGIC_V3, /* New fancy GICv3 */
  79. };
  80. #define LR_STATE_PENDING (1 << 0)
  81. #define LR_STATE_ACTIVE (1 << 1)
  82. #define LR_STATE_MASK (3 << 0)
  83. #define LR_EOI_INT (1 << 2)
  84. struct vgic_lr {
  85. u16 irq;
  86. u8 source;
  87. u8 state;
  88. };
  89. struct vgic_vmcr {
  90. u32 ctlr;
  91. u32 abpr;
  92. u32 bpr;
  93. u32 pmr;
  94. };
  95. struct vgic_ops {
  96. struct vgic_lr (*get_lr)(const struct kvm_vcpu *, int);
  97. void (*set_lr)(struct kvm_vcpu *, int, struct vgic_lr);
  98. void (*sync_lr_elrsr)(struct kvm_vcpu *, int, struct vgic_lr);
  99. u64 (*get_elrsr)(const struct kvm_vcpu *vcpu);
  100. u64 (*get_eisr)(const struct kvm_vcpu *vcpu);
  101. void (*clear_eisr)(struct kvm_vcpu *vcpu);
  102. u32 (*get_interrupt_status)(const struct kvm_vcpu *vcpu);
  103. void (*enable_underflow)(struct kvm_vcpu *vcpu);
  104. void (*disable_underflow)(struct kvm_vcpu *vcpu);
  105. void (*get_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  106. void (*set_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  107. void (*enable)(struct kvm_vcpu *vcpu);
  108. };
  109. struct vgic_params {
  110. /* vgic type */
  111. enum vgic_type type;
  112. /* Physical address of vgic virtual cpu interface */
  113. phys_addr_t vcpu_base;
  114. /* Number of list registers */
  115. u32 nr_lr;
  116. /* Interrupt number */
  117. unsigned int maint_irq;
  118. /* Virtual control interface base address */
  119. void __iomem *vctrl_base;
  120. };
  121. struct vgic_dist {
  122. #ifdef CONFIG_KVM_ARM_VGIC
  123. spinlock_t lock;
  124. bool in_kernel;
  125. bool ready;
  126. int nr_cpus;
  127. int nr_irqs;
  128. /* Virtual control interface mapping */
  129. void __iomem *vctrl_base;
  130. /* Distributor and vcpu interface mapping in the guest */
  131. phys_addr_t vgic_dist_base;
  132. phys_addr_t vgic_cpu_base;
  133. /* Distributor enabled */
  134. u32 enabled;
  135. /* Interrupt enabled (one bit per IRQ) */
  136. struct vgic_bitmap irq_enabled;
  137. /* Level-triggered interrupt external input is asserted */
  138. struct vgic_bitmap irq_level;
  139. /*
  140. * Interrupt state is pending on the distributor
  141. */
  142. struct vgic_bitmap irq_pending;
  143. /*
  144. * Tracks writes to GICD_ISPENDRn and GICD_ICPENDRn for level-triggered
  145. * interrupts. Essentially holds the state of the flip-flop in
  146. * Figure 4-10 on page 4-101 in ARM IHI 0048B.b.
  147. * Once set, it is only cleared for level-triggered interrupts on
  148. * guest ACKs (when we queue it) or writes to GICD_ICPENDRn.
  149. */
  150. struct vgic_bitmap irq_soft_pend;
  151. /* Level-triggered interrupt queued on VCPU interface */
  152. struct vgic_bitmap irq_queued;
  153. /* Interrupt priority. Not used yet. */
  154. struct vgic_bytemap irq_priority;
  155. /* Level/edge triggered */
  156. struct vgic_bitmap irq_cfg;
  157. /*
  158. * Source CPU per SGI and target CPU:
  159. *
  160. * Each byte represent a SGI observable on a VCPU, each bit of
  161. * this byte indicating if the corresponding VCPU has
  162. * generated this interrupt. This is a GICv2 feature only.
  163. *
  164. * For VCPUn (n < 8), irq_sgi_sources[n*16] to [n*16 + 15] are
  165. * the SGIs observable on VCPUn.
  166. */
  167. u8 *irq_sgi_sources;
  168. /*
  169. * Target CPU for each SPI:
  170. *
  171. * Array of available SPI, each byte indicating the target
  172. * VCPU for SPI. IRQn (n >=32) is at irq_spi_cpu[n-32].
  173. */
  174. u8 *irq_spi_cpu;
  175. /*
  176. * Reverse lookup of irq_spi_cpu for faster compute pending:
  177. *
  178. * Array of bitmaps, one per VCPU, describing if IRQn is
  179. * routed to a particular VCPU.
  180. */
  181. struct vgic_bitmap *irq_spi_target;
  182. /* Bitmap indicating which CPU has something pending */
  183. unsigned long *irq_pending_on_cpu;
  184. #endif
  185. };
  186. struct vgic_v2_cpu_if {
  187. u32 vgic_hcr;
  188. u32 vgic_vmcr;
  189. u32 vgic_misr; /* Saved only */
  190. u64 vgic_eisr; /* Saved only */
  191. u64 vgic_elrsr; /* Saved only */
  192. u32 vgic_apr;
  193. u32 vgic_lr[VGIC_V2_MAX_LRS];
  194. };
  195. struct vgic_v3_cpu_if {
  196. #ifdef CONFIG_ARM_GIC_V3
  197. u32 vgic_hcr;
  198. u32 vgic_vmcr;
  199. u32 vgic_misr; /* Saved only */
  200. u32 vgic_eisr; /* Saved only */
  201. u32 vgic_elrsr; /* Saved only */
  202. u32 vgic_ap0r[4];
  203. u32 vgic_ap1r[4];
  204. u64 vgic_lr[VGIC_V3_MAX_LRS];
  205. #endif
  206. };
  207. struct vgic_cpu {
  208. #ifdef CONFIG_KVM_ARM_VGIC
  209. /* per IRQ to LR mapping */
  210. u8 *vgic_irq_lr_map;
  211. /* Pending interrupts on this VCPU */
  212. DECLARE_BITMAP( pending_percpu, VGIC_NR_PRIVATE_IRQS);
  213. unsigned long *pending_shared;
  214. /* Bitmap of used/free list registers */
  215. DECLARE_BITMAP( lr_used, VGIC_V2_MAX_LRS);
  216. /* Number of list registers on this CPU */
  217. int nr_lr;
  218. /* CPU vif control registers for world switch */
  219. union {
  220. struct vgic_v2_cpu_if vgic_v2;
  221. struct vgic_v3_cpu_if vgic_v3;
  222. };
  223. #endif
  224. };
  225. #define LR_EMPTY 0xff
  226. #define INT_STATUS_EOI (1 << 0)
  227. #define INT_STATUS_UNDERFLOW (1 << 1)
  228. struct kvm;
  229. struct kvm_vcpu;
  230. struct kvm_run;
  231. struct kvm_exit_mmio;
  232. #ifdef CONFIG_KVM_ARM_VGIC
  233. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
  234. int kvm_vgic_hyp_init(void);
  235. int kvm_vgic_map_resources(struct kvm *kvm);
  236. int kvm_vgic_create(struct kvm *kvm);
  237. void kvm_vgic_destroy(struct kvm *kvm);
  238. void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
  239. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  240. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  241. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  242. bool level);
  243. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  244. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  245. struct kvm_exit_mmio *mmio);
  246. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.in_kernel))
  247. #define vgic_initialized(k) ((k)->arch.vgic.ready)
  248. int vgic_v2_probe(struct device_node *vgic_node,
  249. const struct vgic_ops **ops,
  250. const struct vgic_params **params);
  251. #ifdef CONFIG_ARM_GIC_V3
  252. int vgic_v3_probe(struct device_node *vgic_node,
  253. const struct vgic_ops **ops,
  254. const struct vgic_params **params);
  255. #else
  256. static inline int vgic_v3_probe(struct device_node *vgic_node,
  257. const struct vgic_ops **ops,
  258. const struct vgic_params **params)
  259. {
  260. return -ENODEV;
  261. }
  262. #endif
  263. #else
  264. static inline int kvm_vgic_hyp_init(void)
  265. {
  266. return 0;
  267. }
  268. static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  269. {
  270. return 0;
  271. }
  272. static inline int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
  273. {
  274. return -ENXIO;
  275. }
  276. static inline int kvm_vgic_map_resources(struct kvm *kvm)
  277. {
  278. return 0;
  279. }
  280. static inline int kvm_vgic_create(struct kvm *kvm)
  281. {
  282. return 0;
  283. }
  284. static inline void kvm_vgic_destroy(struct kvm *kvm)
  285. {
  286. }
  287. static inline void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)
  288. {
  289. }
  290. static inline int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  291. {
  292. return 0;
  293. }
  294. static inline void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) {}
  295. static inline void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) {}
  296. static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid,
  297. unsigned int irq_num, bool level)
  298. {
  299. return 0;
  300. }
  301. static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  302. {
  303. return 0;
  304. }
  305. static inline bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  306. struct kvm_exit_mmio *mmio)
  307. {
  308. return false;
  309. }
  310. static inline int irqchip_in_kernel(struct kvm *kvm)
  311. {
  312. return 0;
  313. }
  314. static inline bool vgic_initialized(struct kvm *kvm)
  315. {
  316. return true;
  317. }
  318. #endif
  319. #endif