arm_vgic.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_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 vgic_dist {
  63. #ifdef CONFIG_KVM_ARM_VGIC
  64. spinlock_t lock;
  65. bool ready;
  66. /* Virtual control interface mapping */
  67. void __iomem *vctrl_base;
  68. /* Distributor and vcpu interface mapping in the guest */
  69. phys_addr_t vgic_dist_base;
  70. phys_addr_t vgic_cpu_base;
  71. /* Distributor enabled */
  72. u32 enabled;
  73. /* Interrupt enabled (one bit per IRQ) */
  74. struct vgic_bitmap irq_enabled;
  75. /* Interrupt 'pin' level */
  76. struct vgic_bitmap irq_state;
  77. /* Level-triggered interrupt in progress */
  78. struct vgic_bitmap irq_active;
  79. /* Interrupt priority. Not used yet. */
  80. struct vgic_bytemap irq_priority;
  81. /* Level/edge triggered */
  82. struct vgic_bitmap irq_cfg;
  83. /* Source CPU per SGI and target CPU */
  84. u8 irq_sgi_sources[VGIC_MAX_CPUS][VGIC_NR_SGIS];
  85. /* Target CPU for each IRQ */
  86. u8 irq_spi_cpu[VGIC_NR_SHARED_IRQS];
  87. struct vgic_bitmap irq_spi_target[VGIC_MAX_CPUS];
  88. /* Bitmap indicating which CPU has something pending */
  89. unsigned long irq_pending_on_cpu;
  90. #endif
  91. };
  92. struct vgic_cpu {
  93. #ifdef CONFIG_KVM_ARM_VGIC
  94. /* per IRQ to LR mapping */
  95. u8 vgic_irq_lr_map[VGIC_NR_IRQS];
  96. /* Pending interrupts on this VCPU */
  97. DECLARE_BITMAP( pending_percpu, VGIC_NR_PRIVATE_IRQS);
  98. DECLARE_BITMAP( pending_shared, VGIC_NR_SHARED_IRQS);
  99. /* Bitmap of used/free list registers */
  100. DECLARE_BITMAP( lr_used, VGIC_MAX_LRS);
  101. /* Number of list registers on this CPU */
  102. int nr_lr;
  103. /* CPU vif control registers for world switch */
  104. u32 vgic_hcr;
  105. u32 vgic_vmcr;
  106. u32 vgic_misr; /* Saved only */
  107. u32 vgic_eisr[2]; /* Saved only */
  108. u32 vgic_elrsr[2]; /* Saved only */
  109. u32 vgic_apr;
  110. u32 vgic_lr[VGIC_MAX_LRS];
  111. #endif
  112. };
  113. #define LR_EMPTY 0xff
  114. struct kvm;
  115. struct kvm_vcpu;
  116. struct kvm_run;
  117. struct kvm_exit_mmio;
  118. #ifdef CONFIG_KVM_ARM_VGIC
  119. int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr);
  120. int kvm_vgic_hyp_init(void);
  121. int kvm_vgic_init(struct kvm *kvm);
  122. int kvm_vgic_create(struct kvm *kvm);
  123. int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
  124. void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
  125. void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
  126. int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int irq_num,
  127. bool level);
  128. int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
  129. bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  130. struct kvm_exit_mmio *mmio);
  131. #define irqchip_in_kernel(k) (!!((k)->arch.vgic.vctrl_base))
  132. #define vgic_initialized(k) ((k)->arch.vgic.ready)
  133. #else
  134. static inline int kvm_vgic_hyp_init(void)
  135. {
  136. return 0;
  137. }
  138. static inline int kvm_vgic_set_addr(struct kvm *kvm, unsigned long type, u64 addr)
  139. {
  140. return 0;
  141. }
  142. static inline int kvm_vgic_init(struct kvm *kvm)
  143. {
  144. return 0;
  145. }
  146. static inline int kvm_vgic_create(struct kvm *kvm)
  147. {
  148. return 0;
  149. }
  150. static inline int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
  151. {
  152. return 0;
  153. }
  154. static inline void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu) {}
  155. static inline void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu) {}
  156. static inline int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid,
  157. unsigned int irq_num, bool level)
  158. {
  159. return 0;
  160. }
  161. static inline int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
  162. {
  163. return 0;
  164. }
  165. static inline bool vgic_handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *run,
  166. struct kvm_exit_mmio *mmio)
  167. {
  168. return false;
  169. }
  170. static inline int irqchip_in_kernel(struct kvm *kvm)
  171. {
  172. return 0;
  173. }
  174. static inline bool vgic_initialized(struct kvm *kvm)
  175. {
  176. return true;
  177. }
  178. #endif
  179. #endif