arm_vgic.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include <linux/irqchip/arm-gic.h>
  26. #define VGIC_NR_IRQS 256
  27. #define VGIC_NR_SGIS 16
  28. #define VGIC_NR_PPIS 16
  29. #define VGIC_NR_PRIVATE_IRQS (VGIC_NR_SGIS + VGIC_NR_PPIS)
  30. #define VGIC_NR_SHARED_IRQS (VGIC_NR_IRQS - VGIC_NR_PRIVATE_IRQS)
  31. #define VGIC_MAX_CPUS KVM_MAX_VCPUS
  32. #define VGIC_V2_MAX_LRS (1 << 6)
  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. #define LR_STATE_PENDING (1 << 0)
  64. #define LR_STATE_ACTIVE (1 << 1)
  65. #define LR_STATE_MASK (3 << 0)
  66. #define LR_EOI_INT (1 << 2)
  67. struct vgic_lr {
  68. u16 irq;
  69. u8 source;
  70. u8 state;
  71. };
  72. struct vgic_vmcr {
  73. u32 ctlr;
  74. u32 abpr;
  75. u32 bpr;
  76. u32 pmr;
  77. };
  78. struct vgic_ops {
  79. struct vgic_lr (*get_lr)(const struct kvm_vcpu *, int);
  80. void (*set_lr)(struct kvm_vcpu *, int, struct vgic_lr);
  81. void (*sync_lr_elrsr)(struct kvm_vcpu *, int, struct vgic_lr);
  82. u64 (*get_elrsr)(const struct kvm_vcpu *vcpu);
  83. u64 (*get_eisr)(const struct kvm_vcpu *vcpu);
  84. u32 (*get_interrupt_status)(const struct kvm_vcpu *vcpu);
  85. void (*enable_underflow)(struct kvm_vcpu *vcpu);
  86. void (*disable_underflow)(struct kvm_vcpu *vcpu);
  87. void (*get_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  88. void (*set_vmcr)(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
  89. void (*enable)(struct kvm_vcpu *vcpu);
  90. };
  91. struct vgic_params {
  92. /* Physical address of vgic virtual cpu interface */
  93. phys_addr_t vcpu_base;
  94. /* Number of list registers */
  95. u32 nr_lr;
  96. /* Interrupt number */
  97. unsigned int maint_irq;
  98. /* Virtual control interface base address */
  99. void __iomem *vctrl_base;
  100. };
  101. struct vgic_dist {
  102. #ifdef CONFIG_KVM_ARM_VGIC
  103. spinlock_t lock;
  104. bool ready;
  105. /* Virtual control interface mapping */
  106. void __iomem *vctrl_base;
  107. /* Distributor and vcpu interface mapping in the guest */
  108. phys_addr_t vgic_dist_base;
  109. phys_addr_t vgic_cpu_base;
  110. /* Distributor enabled */
  111. u32 enabled;
  112. /* Interrupt enabled (one bit per IRQ) */
  113. struct vgic_bitmap irq_enabled;
  114. /* Interrupt 'pin' level */
  115. struct vgic_bitmap irq_state;
  116. /* Level-triggered interrupt in progress */
  117. struct vgic_bitmap irq_active;
  118. /* Interrupt priority. Not used yet. */
  119. struct vgic_bytemap irq_priority;
  120. /* Level/edge triggered */
  121. struct vgic_bitmap irq_cfg;
  122. /* Source CPU per SGI and target CPU */
  123. u8 irq_sgi_sources[VGIC_MAX_CPUS][VGIC_NR_SGIS];
  124. /* Target CPU for each IRQ */
  125. u8 irq_spi_cpu[VGIC_NR_SHARED_IRQS];
  126. struct vgic_bitmap irq_spi_target[VGIC_MAX_CPUS];
  127. /* Bitmap indicating which CPU has something pending */
  128. unsigned long irq_pending_on_cpu;
  129. #endif
  130. };
  131. struct vgic_v2_cpu_if {
  132. u32 vgic_hcr;
  133. u32 vgic_vmcr;
  134. u32 vgic_misr; /* Saved only */
  135. u32 vgic_eisr[2]; /* Saved only */
  136. u32 vgic_elrsr[2]; /* Saved only */
  137. u32 vgic_apr;
  138. u32 vgic_lr[VGIC_V2_MAX_LRS];
  139. };
  140. struct vgic_cpu {
  141. #ifdef CONFIG_KVM_ARM_VGIC
  142. /* per IRQ to LR mapping */
  143. u8 vgic_irq_lr_map[VGIC_NR_IRQS];
  144. /* Pending interrupts on this VCPU */
  145. DECLARE_BITMAP( pending_percpu, VGIC_NR_PRIVATE_IRQS);
  146. DECLARE_BITMAP( pending_shared, VGIC_NR_SHARED_IRQS);
  147. /* Bitmap of used/free list registers */
  148. DECLARE_BITMAP( lr_used, VGIC_V2_MAX_LRS);
  149. /* Number of list registers on this CPU */
  150. int nr_lr;
  151. /* CPU vif control registers for world switch */
  152. union {
  153. struct vgic_v2_cpu_if vgic_v2;
  154. };
  155. #endif
  156. };
  157. #define LR_EMPTY 0xff
  158. #define INT_STATUS_EOI (1 << 0)
  159. #define INT_STATUS_UNDERFLOW (1 << 1)
  160. struct kvm;
  161. struct kvm_vcpu;
  162. struct kvm_run;
  163. struct kvm_exit_mmio;
  164. #ifdef CONFIG_KVM_ARM_VGIC
  165. int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
  166. int kvm_vgic_hyp_init(void);
  167. int kvm_vgic_init(struct kvm *kvm);
  168. int kvm_vgic_create(struct kvm *kvm);
  169. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
  170. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  171. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  172. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  173. bool level);
  174. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  175. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  176. struct kvm_exit_mmio *mmio);
  177. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.vctrl_base))
  178. #define vgic_initialized(k) ((k)->arch.vgic.ready)
  179. int vgic_v2_probe(struct device_node *vgic_node,
  180. const struct vgic_ops **ops,
  181. const struct vgic_params **params);
  182. #else
  183. static inline int kvm_vgic_hyp_init(void)
  184. {
  185. return 0;
  186. }
  187. static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  188. {
  189. return 0;
  190. }
  191. static inline int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write)
  192. {
  193. return -ENXIO;
  194. }
  195. static inline int kvm_vgic_init(struct kvm *kvm)
  196. {
  197. return 0;
  198. }
  199. static inline int kvm_vgic_create(struct kvm *kvm)
  200. {
  201. return 0;
  202. }
  203. static inline int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  204. {
  205. return 0;
  206. }
  207. static inline void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) {}
  208. static inline void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) {}
  209. static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid,
  210. unsigned int irq_num, bool level)
  211. {
  212. return 0;
  213. }
  214. static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  215. {
  216. return 0;
  217. }
  218. static inline bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  219. struct kvm_exit_mmio *mmio)
  220. {
  221. return false;
  222. }
  223. static inline int irqchip_in_kernel(struct kvm *kvm)
  224. {
  225. return 0;
  226. }
  227. static inline bool vgic_initialized(struct kvm *kvm)
  228. {
  229. return true;
  230. }
  231. #endif
  232. #endif