arm_vgic.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 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_NR_SHARED_IRQS (VGIC_NR_IRQS - VGIC_NR_PRIVATE_IRQS)
  30. #define VGIC_MAX_CPUS KVM_MAX_VCPUS
  31. #define VGIC_V2_MAX_LRS (1 << 6)
  32. #define VGIC_V3_MAX_LRS 16
  33. /* Sanity checks... */
  34. #if (VGIC_MAX_CPUS > 8)
  35. #error Invalid number of CPU interfaces
  36. #endif
  37. #if (VGIC_NR_IRQS & 31)
  38. #error "VGIC_NR_IRQS must be a multiple of 32"
  39. #endif
  40. #if (VGIC_NR_IRQS > 1024)
  41. #error "VGIC_NR_IRQS must be <= 1024"
  42. #endif
  43. /*
  44. * The GIC distributor registers describing interrupts have two parts:
  45. * - 32 per-CPU interrupts (SGI + PPI)
  46. * - a bunch of shared interrupts (SPI)
  47. */
  48. struct vgic_bitmap {
  49. union {
  50. u32 reg[VGIC_NR_PRIVATE_IRQS / 32];
  51. DECLARE_BITMAP(reg_ul, VGIC_NR_PRIVATE_IRQS);
  52. } percpu[VGIC_MAX_CPUS];
  53. union {
  54. u32 reg[VGIC_NR_SHARED_IRQS / 32];
  55. DECLARE_BITMAP(reg_ul, VGIC_NR_SHARED_IRQS);
  56. } shared;
  57. };
  58. struct vgic_bytemap {
  59. u32 percpu[VGIC_MAX_CPUS][VGIC_NR_PRIVATE_IRQS / 4];
  60. u32 shared[VGIC_NR_SHARED_IRQS / 4];
  61. };
  62. struct kvm_vcpu;
  63. enum vgic_type {
  64. VGIC_V2, /* Good ol' GICv2 */
  65. VGIC_V3, /* New fancy GICv3 */
  66. };
  67. #define LR_STATE_PENDING (1 << 0)
  68. #define LR_STATE_ACTIVE (1 << 1)
  69. #define LR_STATE_MASK (3 << 0)
  70. #define LR_EOI_INT (1 << 2)
  71. struct vgic_lr {
  72. u16 irq;
  73. u8 source;
  74. u8 state;
  75. };
  76. struct vgic_vmcr {
  77. u32 ctlr;
  78. u32 abpr;
  79. u32 bpr;
  80. u32 pmr;
  81. };
  82. struct vgic_ops {
  83. struct vgic_lr (*get_lr)(const struct kvm_vcpu *, int);
  84. void (*set_lr)(struct kvm_vcpu *, int, struct vgic_lr);
  85. void (*sync_lr_elrsr)(struct kvm_vcpu *, int, struct vgic_lr);
  86. u64 (*get_elrsr)(const struct kvm_vcpu *vcpu);
  87. u64 (*get_eisr)(const struct kvm_vcpu *vcpu);
  88. u32 (*get_interrupt_status)(const struct kvm_vcpu *vcpu);
  89. void (*enable_underflow)(struct kvm_vcpu *vcpu);
  90. void (*disable_underflow)(struct kvm_vcpu *vcpu);
  91. void (*get_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  92. void (*set_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  93. void (*enable)(struct kvm_vcpu *vcpu);
  94. };
  95. struct vgic_params {
  96. /* vgic type */
  97. enum vgic_type type;
  98. /* Physical address of vgic virtual cpu interface */
  99. phys_addr_t vcpu_base;
  100. /* Number of list registers */
  101. u32 nr_lr;
  102. /* Interrupt number */
  103. unsigned int maint_irq;
  104. /* Virtual control interface base address */
  105. void __iomem *vctrl_base;
  106. };
  107. struct vgic_dist {
  108. #ifdef CONFIG_KVM_ARM_VGIC
  109. spinlock_t lock;
  110. bool in_kernel;
  111. bool ready;
  112. /* Virtual control interface mapping */
  113. void __iomem *vctrl_base;
  114. /* Distributor and vcpu interface mapping in the guest */
  115. phys_addr_t vgic_dist_base;
  116. phys_addr_t vgic_cpu_base;
  117. /* Distributor enabled */
  118. u32 enabled;
  119. /* Interrupt enabled (one bit per IRQ) */
  120. struct vgic_bitmap irq_enabled;
  121. /* Interrupt 'pin' level */
  122. struct vgic_bitmap irq_state;
  123. /* Level-triggered interrupt in progress */
  124. struct vgic_bitmap irq_active;
  125. /* Interrupt priority. Not used yet. */
  126. struct vgic_bytemap irq_priority;
  127. /* Level/edge triggered */
  128. struct vgic_bitmap irq_cfg;
  129. /* Source CPU per SGI and target CPU */
  130. u8 irq_sgi_sources[VGIC_MAX_CPUS][VGIC_NR_SGIS];
  131. /* Target CPU for each IRQ */
  132. u8 irq_spi_cpu[VGIC_NR_SHARED_IRQS];
  133. struct vgic_bitmap irq_spi_target[VGIC_MAX_CPUS];
  134. /* Bitmap indicating which CPU has something pending */
  135. unsigned long irq_pending_on_cpu;
  136. #endif
  137. };
  138. struct vgic_v2_cpu_if {
  139. u32 vgic_hcr;
  140. u32 vgic_vmcr;
  141. u32 vgic_misr; /* Saved only */
  142. u32 vgic_eisr[2]; /* Saved only */
  143. u32 vgic_elrsr[2]; /* Saved only */
  144. u32 vgic_apr;
  145. u32 vgic_lr[VGIC_V2_MAX_LRS];
  146. };
  147. struct vgic_v3_cpu_if {
  148. #ifdef CONFIG_ARM_GIC_V3
  149. u32 vgic_hcr;
  150. u32 vgic_vmcr;
  151. u32 vgic_misr; /* Saved only */
  152. u32 vgic_eisr; /* Saved only */
  153. u32 vgic_elrsr; /* Saved only */
  154. u32 vgic_ap0r[4];
  155. u32 vgic_ap1r[4];
  156. u64 vgic_lr[VGIC_V3_MAX_LRS];
  157. #endif
  158. };
  159. struct vgic_cpu {
  160. #ifdef CONFIG_KVM_ARM_VGIC
  161. /* per IRQ to LR mapping */
  162. u8 vgic_irq_lr_map[VGIC_NR_IRQS];
  163. /* Pending interrupts on this VCPU */
  164. DECLARE_BITMAP( pending_percpu, VGIC_NR_PRIVATE_IRQS);
  165. DECLARE_BITMAP( pending_shared, VGIC_NR_SHARED_IRQS);
  166. /* Bitmap of used/free list registers */
  167. DECLARE_BITMAP( lr_used, VGIC_V2_MAX_LRS);
  168. /* Number of list registers on this CPU */
  169. int nr_lr;
  170. /* CPU vif control registers for world switch */
  171. union {
  172. struct vgic_v2_cpu_if vgic_v2;
  173. struct vgic_v3_cpu_if vgic_v3;
  174. };
  175. #endif
  176. };
  177. #define LR_EMPTY 0xff
  178. #define INT_STATUS_EOI (1 << 0)
  179. #define INT_STATUS_UNDERFLOW (1 << 1)
  180. struct kvm;
  181. struct kvm_vcpu;
  182. struct kvm_run;
  183. struct kvm_exit_mmio;
  184. #ifdef CONFIG_KVM_ARM_VGIC
  185. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
  186. int kvm_vgic_hyp_init(void);
  187. int kvm_vgic_init(struct kvm *kvm);
  188. int kvm_vgic_create(struct kvm *kvm);
  189. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
  190. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  191. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  192. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  193. bool level);
  194. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  195. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  196. struct kvm_exit_mmio *mmio);
  197. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.in_kernel))
  198. #define vgic_initialized(k) ((k)->arch.vgic.ready)
  199. int vgic_v2_probe(struct device_node *vgic_node,
  200. const struct vgic_ops **ops,
  201. const struct vgic_params **params);
  202. #ifdef CONFIG_ARM_GIC_V3
  203. int vgic_v3_probe(struct device_node *vgic_node,
  204. const struct vgic_ops **ops,
  205. const struct vgic_params **params);
  206. #else
  207. static inline int vgic_v3_probe(struct device_node *vgic_node,
  208. const struct vgic_ops **ops,
  209. const struct vgic_params **params)
  210. {
  211. return -ENODEV;
  212. }
  213. #endif
  214. #else
  215. static inline int kvm_vgic_hyp_init(void)
  216. {
  217. return 0;
  218. }
  219. static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  220. {
  221. return 0;
  222. }
  223. static inline int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
  224. {
  225. return -ENXIO;
  226. }
  227. static inline int kvm_vgic_init(struct kvm *kvm)
  228. {
  229. return 0;
  230. }
  231. static inline int kvm_vgic_create(struct kvm *kvm)
  232. {
  233. return 0;
  234. }
  235. static inline int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  236. {
  237. return 0;
  238. }
  239. static inline void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) {}
  240. static inline void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) {}
  241. static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid,
  242. unsigned int irq_num, bool level)
  243. {
  244. return 0;
  245. }
  246. static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  247. {
  248. return 0;
  249. }
  250. static inline bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  251. struct kvm_exit_mmio *mmio)
  252. {
  253. return false;
  254. }
  255. static inline int irqchip_in_kernel(struct kvm *kvm)
  256. {
  257. return 0;
  258. }
  259. static inline bool vgic_initialized(struct kvm *kvm)
  260. {
  261. return true;
  262. }
  263. #endif
  264. #endif