arm_vgic.h 10 KB

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