kvm_vgic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/kvm_host.h>
  23. #include <linux/irqreturn.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/types.h>
  26. #include <linux/irqchip/arm-gic.h>
  27. #define VGIC_NR_IRQS 128
  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_NR_SHARED_IRQS (VGIC_NR_IRQS - VGIC_NR_PRIVATE_IRQS)
  32. #define VGIC_MAX_CPUS KVM_MAX_VCPUS
  33. #define VGIC_MAX_LRS (1 << 6)
  34. /* Sanity checks... */
  35. #if (VGIC_MAX_CPUS > 8)
  36. #error Invalid number of CPU interfaces
  37. #endif
  38. #if (VGIC_NR_IRQS & 31)
  39. #error "VGIC_NR_IRQS must be a multiple of 32"
  40. #endif
  41. #if (VGIC_NR_IRQS > 1024)
  42. #error "VGIC_NR_IRQS must be <= 1024"
  43. #endif
  44. /*
  45. * The GIC distributor registers describing interrupts have two parts:
  46. * - 32 per-CPU interrupts (SGI + PPI)
  47. * - a bunch of shared interrupts (SPI)
  48. */
  49. struct vgic_bitmap {
  50. union {
  51. u32 reg[VGIC_NR_PRIVATE_IRQS / 32];
  52. DECLARE_BITMAP(reg_ul, VGIC_NR_PRIVATE_IRQS);
  53. } percpu[VGIC_MAX_CPUS];
  54. union {
  55. u32 reg[VGIC_NR_SHARED_IRQS / 32];
  56. DECLARE_BITMAP(reg_ul, VGIC_NR_SHARED_IRQS);
  57. } shared;
  58. };
  59. struct vgic_bytemap {
  60. u32 percpu[VGIC_MAX_CPUS][VGIC_NR_PRIVATE_IRQS / 4];
  61. u32 shared[VGIC_NR_SHARED_IRQS / 4];
  62. };
  63. struct vgic_dist {
  64. #ifdef CONFIG_KVM_ARM_VGIC
  65. spinlock_t lock;
  66. bool ready;
  67. /* Virtual control interface mapping */
  68. void __iomem *vctrl_base;
  69. /* Distributor and vcpu interface mapping in the guest */
  70. phys_addr_t vgic_dist_base;
  71. phys_addr_t vgic_cpu_base;
  72. /* Distributor enabled */
  73. u32 enabled;
  74. /* Interrupt enabled (one bit per IRQ) */
  75. struct vgic_bitmap irq_enabled;
  76. /* Interrupt 'pin' level */
  77. struct vgic_bitmap irq_state;
  78. /* Level-triggered interrupt in progress */
  79. struct vgic_bitmap irq_active;
  80. /* Interrupt priority. Not used yet. */
  81. struct vgic_bytemap irq_priority;
  82. /* Level/edge triggered */
  83. struct vgic_bitmap irq_cfg;
  84. /* Source CPU per SGI and target CPU */
  85. u8 irq_sgi_sources[VGIC_MAX_CPUS][VGIC_NR_SGIS];
  86. /* Target CPU for each IRQ */
  87. u8 irq_spi_cpu[VGIC_NR_SHARED_IRQS];
  88. struct vgic_bitmap irq_spi_target[VGIC_MAX_CPUS];
  89. /* Bitmap indicating which CPU has something pending */
  90. unsigned long irq_pending_on_cpu;
  91. #endif
  92. };
  93. struct vgic_cpu {
  94. #ifdef CONFIG_KVM_ARM_VGIC
  95. /* per IRQ to LR mapping */
  96. u8 vgic_irq_lr_map[VGIC_NR_IRQS];
  97. /* Pending interrupts on this VCPU */
  98. DECLARE_BITMAP( pending_percpu, VGIC_NR_PRIVATE_IRQS);
  99. DECLARE_BITMAP( pending_shared, VGIC_NR_SHARED_IRQS);
  100. /* Bitmap of used/free list registers */
  101. DECLARE_BITMAP( lr_used, VGIC_MAX_LRS);
  102. /* Number of list registers on this CPU */
  103. int nr_lr;
  104. /* CPU vif control registers for world switch */
  105. u32 vgic_hcr;
  106. u32 vgic_vmcr;
  107. u32 vgic_misr; /* Saved only */
  108. u32 vgic_eisr[2]; /* Saved only */
  109. u32 vgic_elrsr[2]; /* Saved only */
  110. u32 vgic_apr;
  111. u32 vgic_lr[VGIC_MAX_LRS];
  112. #endif
  113. };
  114. #define LR_EMPTY 0xff
  115. struct kvm;
  116. struct kvm_vcpu;
  117. struct kvm_run;
  118. struct kvm_exit_mmio;
  119. #ifdef CONFIG_KVM_ARM_VGIC
  120. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr);
  121. int kvm_vgic_hyp_init(void);
  122. int kvm_vgic_init(struct kvm *kvm);
  123. int kvm_vgic_create(struct kvm *kvm);
  124. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
  125. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  126. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  127. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  128. bool level);
  129. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  130. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  131. struct kvm_exit_mmio *mmio);
  132. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.vctrl_base))
  133. #define vgic_initialized(k) ((k)->arch.vgic.ready)
  134. #else
  135. static inline int kvm_vgic_hyp_init(void)
  136. {
  137. return 0;
  138. }
  139. static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  140. {
  141. return 0;
  142. }
  143. static inline int kvm_vgic_init(struct kvm *kvm)
  144. {
  145. return 0;
  146. }
  147. static inline int kvm_vgic_create(struct kvm *kvm)
  148. {
  149. return 0;
  150. }
  151. static inline int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  152. {
  153. return 0;
  154. }
  155. static inline void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) {}
  156. static inline void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) {}
  157. static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid,
  158. unsigned int irq_num, bool level)
  159. {
  160. return 0;
  161. }
  162. static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  163. {
  164. return 0;
  165. }
  166. static inline bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  167. struct kvm_exit_mmio *mmio)
  168. {
  169. return false;
  170. }
  171. static inline int irqchip_in_kernel(struct kvm *kvm)
  172. {
  173. return 0;
  174. }
  175. static inline bool vgic_initialized(struct kvm *kvm)
  176. {
  177. return true;
  178. }
  179. #endif
  180. #endif