arm_gicv3_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * ARM GIC support
  3. *
  4. * Copyright (c) 2012 Linaro Limited
  5. * Copyright (c) 2015 Huawei.
  6. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  7. * Written by Peter Maydell
  8. * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #ifndef HW_ARM_GICV3_COMMON_H
  24. #define HW_ARM_GICV3_COMMON_H
  25. #include "hw/sysbus.h"
  26. #include "hw/intc/arm_gic_common.h"
  27. #include "qom/object.h"
  28. /*
  29. * Maximum number of possible interrupts, determined by the GIC architecture.
  30. * Note that this does not include LPIs. When implemented, these should be
  31. * dealt with separately.
  32. */
  33. #define GICV3_MAXIRQ 1020
  34. #define GICV3_MAXSPI (GICV3_MAXIRQ - GIC_INTERNAL)
  35. #define GICV3_LPI_INTID_START 8192
  36. /*
  37. * The redistributor in GICv3 has two 64KB frames per CPU; in
  38. * GICv4 it has four 64KB frames per CPU.
  39. */
  40. #define GICV3_REDIST_SIZE 0x20000
  41. #define GICV4_REDIST_SIZE 0x40000
  42. /* Number of SGI target-list bits */
  43. #define GICV3_TARGETLIST_BITS 16
  44. /* Maximum number of list registers (architectural limit) */
  45. #define GICV3_LR_MAX 16
  46. /*
  47. * For some distributor fields we want to model the array of 32-bit
  48. * register values which hold various bitmaps corresponding to enabled,
  49. * pending, etc bits. We use the set_bit32() etc family of functions
  50. * from bitops.h for this. For a few cases we need to implement some
  51. * extra operations.
  52. *
  53. * Each bitmap contains a bit for each interrupt. Although there is
  54. * space for the PPIs and SGIs, those bits (the first 32) are never
  55. * used as that state lives in the redistributor. The unused bits are
  56. * provided purely so that interrupt X's state is always in bit X; this
  57. * avoids bugs where we forget to subtract GIC_INTERNAL from an
  58. * interrupt number.
  59. */
  60. #define GIC_DECLARE_BITMAP(name) DECLARE_BITMAP32(name, GICV3_MAXIRQ)
  61. #define GICV3_BMP_SIZE BITS_TO_U32S(GICV3_MAXIRQ)
  62. static inline void gic_bmp_replace_bit(int nr, uint32_t *addr, int val)
  63. {
  64. uint32_t mask = BIT32_MASK(nr);
  65. uint32_t *p = addr + BIT32_WORD(nr);
  66. *p &= ~mask;
  67. *p |= (val & 1U) << (nr % 32);
  68. }
  69. /* Return a pointer to the 32-bit word containing the specified bit. */
  70. static inline uint32_t *gic_bmp_ptr32(uint32_t *addr, int nr)
  71. {
  72. return addr + BIT32_WORD(nr);
  73. }
  74. typedef struct GICv3State GICv3State;
  75. typedef struct GICv3CPUState GICv3CPUState;
  76. /* Some CPU interface registers come in three flavours:
  77. * Group0, Group1 (Secure) and Group1 (NonSecure)
  78. * (where the latter two are exposed as a single banked system register).
  79. * In the state struct they are implemented as a 3-element array which
  80. * can be indexed into by the GICV3_G0, GICV3_G1 and GICV3_G1NS constants.
  81. * If the CPU doesn't support EL3 then the G1 element is unused.
  82. *
  83. * These constants are also used to communicate the group to use for
  84. * an interrupt or SGI when it is passed between the cpu interface and
  85. * the redistributor or distributor. For those purposes the receiving end
  86. * must be prepared to cope with a Group 1 Secure interrupt even if it does
  87. * not have security support enabled, because security can be disabled
  88. * independently in the CPU and in the GIC. In that case the receiver should
  89. * treat an incoming Group 1 Secure interrupt as if it were Group 0.
  90. * (This architectural requirement is why the _G1 element is the unused one
  91. * in a no-EL3 CPU: we would otherwise have to translate back and forth
  92. * between (G0, G1NS) from the distributor and (G0, G1) in the CPU i/f.)
  93. */
  94. #define GICV3_G0 0
  95. #define GICV3_G1 1
  96. #define GICV3_G1NS 2
  97. /* ICC_CTLR_EL1, GICD_STATUSR and GICR_STATUSR are banked but not
  98. * group-related, so those indices are just 0 for S and 1 for NS.
  99. * (If the CPU or the GIC, respectively, don't support the Security
  100. * extensions then the S element is unused.)
  101. */
  102. #define GICV3_S 0
  103. #define GICV3_NS 1
  104. typedef struct {
  105. int irq;
  106. uint8_t prio;
  107. int grp;
  108. bool nmi;
  109. } PendingIrq;
  110. struct GICv3CPUState {
  111. GICv3State *gic;
  112. CPUState *cpu;
  113. qemu_irq parent_irq;
  114. qemu_irq parent_fiq;
  115. qemu_irq parent_virq;
  116. qemu_irq parent_vfiq;
  117. qemu_irq parent_nmi;
  118. qemu_irq parent_vnmi;
  119. /* Redistributor */
  120. uint32_t level; /* Current IRQ level */
  121. /* RD_base page registers */
  122. uint32_t gicr_ctlr;
  123. uint64_t gicr_typer;
  124. uint32_t gicr_statusr[2];
  125. uint32_t gicr_waker;
  126. uint64_t gicr_propbaser;
  127. uint64_t gicr_pendbaser;
  128. /* SGI_base page registers */
  129. uint32_t gicr_igroupr0;
  130. uint32_t gicr_ienabler0;
  131. uint32_t gicr_ipendr0;
  132. uint32_t gicr_iactiver0;
  133. uint32_t gicr_inmir0;
  134. uint32_t edge_trigger; /* ICFGR0 and ICFGR1 even bits */
  135. uint32_t gicr_igrpmodr0;
  136. uint32_t gicr_nsacr;
  137. uint8_t gicr_ipriorityr[GIC_INTERNAL];
  138. /* VLPI_base page registers */
  139. uint64_t gicr_vpropbaser;
  140. uint64_t gicr_vpendbaser;
  141. /* CPU interface */
  142. uint64_t icc_sre_el1;
  143. uint64_t icc_ctlr_el1[2];
  144. uint64_t icc_pmr_el1;
  145. uint64_t icc_bpr[3];
  146. uint64_t icc_apr[3][4];
  147. uint64_t icc_igrpen[3];
  148. uint64_t icc_ctlr_el3;
  149. /* Virtualization control interface */
  150. uint64_t ich_apr[3][4]; /* ich_apr[GICV3_G1][x] never used */
  151. uint64_t ich_hcr_el2;
  152. uint64_t ich_lr_el2[GICV3_LR_MAX];
  153. uint64_t ich_vmcr_el2;
  154. /* Properties of the CPU interface. These are initialized from
  155. * the settings in the CPU proper.
  156. * If the number of implemented list registers is 0 then the
  157. * virtualization support is not implemented.
  158. */
  159. int num_list_regs;
  160. int vpribits; /* number of virtual priority bits */
  161. int vprebits; /* number of virtual preemption bits */
  162. int pribits; /* number of physical priority bits */
  163. int prebits; /* number of physical preemption bits */
  164. /* Current highest priority pending interrupt for this CPU.
  165. * This is cached information that can be recalculated from the
  166. * real state above; it doesn't need to be migrated.
  167. */
  168. PendingIrq hppi;
  169. /*
  170. * Cached information recalculated from LPI tables
  171. * in guest memory
  172. */
  173. PendingIrq hpplpi;
  174. /* Cached information recalculated from vLPI tables in guest memory */
  175. PendingIrq hppvlpi;
  176. /* This is temporary working state, to avoid a malloc in gicv3_update() */
  177. bool seenbetter;
  178. /*
  179. * Whether the CPU interface has NMI support (FEAT_GICv3_NMI). The
  180. * CPU interface may support NMIs even when the GIC proper (what the
  181. * spec calls the IRI; the redistributors and distributor) does not.
  182. */
  183. bool nmi_support;
  184. };
  185. /*
  186. * The redistributor pages might be split into more than one region
  187. * on some machine types if there are many CPUs.
  188. */
  189. typedef struct GICv3RedistRegion {
  190. GICv3State *gic;
  191. MemoryRegion iomem;
  192. uint32_t cpuidx; /* index of first CPU this region covers */
  193. } GICv3RedistRegion;
  194. struct GICv3State {
  195. /*< private >*/
  196. SysBusDevice parent_obj;
  197. /*< public >*/
  198. MemoryRegion iomem_dist; /* Distributor */
  199. GICv3RedistRegion *redist_regions; /* Redistributor Regions */
  200. uint32_t *redist_region_count; /* redistributor count within each region */
  201. uint32_t nb_redist_regions; /* number of redist regions */
  202. uint32_t num_cpu;
  203. uint32_t num_irq;
  204. uint32_t revision;
  205. bool lpi_enable;
  206. bool nmi_support;
  207. bool security_extn;
  208. bool force_8bit_prio;
  209. bool irq_reset_nonsecure;
  210. bool gicd_no_migration_shift_bug;
  211. int dev_fd; /* kvm device fd if backed by kvm vgic support */
  212. Error *migration_blocker;
  213. MemoryRegion *dma;
  214. AddressSpace dma_as;
  215. /* Distributor */
  216. /* for a GIC with the security extensions the NS banked version of this
  217. * register is just an alias of bit 1 of the S banked version.
  218. */
  219. uint32_t gicd_ctlr;
  220. uint32_t gicd_statusr[2];
  221. GIC_DECLARE_BITMAP(group); /* GICD_IGROUPR */
  222. GIC_DECLARE_BITMAP(grpmod); /* GICD_IGRPMODR */
  223. GIC_DECLARE_BITMAP(enabled); /* GICD_ISENABLER */
  224. GIC_DECLARE_BITMAP(pending); /* GICD_ISPENDR */
  225. GIC_DECLARE_BITMAP(active); /* GICD_ISACTIVER */
  226. GIC_DECLARE_BITMAP(level); /* Current level */
  227. GIC_DECLARE_BITMAP(edge_trigger); /* GICD_ICFGR even bits */
  228. GIC_DECLARE_BITMAP(nmi); /* GICD_INMIR */
  229. uint8_t gicd_ipriority[GICV3_MAXIRQ];
  230. uint64_t gicd_irouter[GICV3_MAXIRQ];
  231. /* Cached information: pointer to the cpu i/f for the CPUs specified
  232. * in the IROUTER registers
  233. */
  234. GICv3CPUState *gicd_irouter_target[GICV3_MAXIRQ];
  235. uint32_t gicd_nsacr[DIV_ROUND_UP(GICV3_MAXIRQ, 16)];
  236. GICv3CPUState *cpu;
  237. /* List of all ITSes connected to this GIC */
  238. GPtrArray *itslist;
  239. };
  240. #define GICV3_BITMAP_ACCESSORS(BMP) \
  241. static inline void gicv3_gicd_##BMP##_set(GICv3State *s, int irq) \
  242. { \
  243. set_bit32(irq, s->BMP); \
  244. } \
  245. static inline int gicv3_gicd_##BMP##_test(GICv3State *s, int irq) \
  246. { \
  247. return test_bit32(irq, s->BMP); \
  248. } \
  249. static inline void gicv3_gicd_##BMP##_clear(GICv3State *s, int irq) \
  250. { \
  251. clear_bit32(irq, s->BMP); \
  252. } \
  253. static inline void gicv3_gicd_##BMP##_replace(GICv3State *s, \
  254. int irq, int value) \
  255. { \
  256. gic_bmp_replace_bit(irq, s->BMP, value); \
  257. }
  258. GICV3_BITMAP_ACCESSORS(group)
  259. GICV3_BITMAP_ACCESSORS(grpmod)
  260. GICV3_BITMAP_ACCESSORS(enabled)
  261. GICV3_BITMAP_ACCESSORS(pending)
  262. GICV3_BITMAP_ACCESSORS(active)
  263. GICV3_BITMAP_ACCESSORS(level)
  264. GICV3_BITMAP_ACCESSORS(edge_trigger)
  265. GICV3_BITMAP_ACCESSORS(nmi)
  266. #define TYPE_ARM_GICV3_COMMON "arm-gicv3-common"
  267. typedef struct ARMGICv3CommonClass ARMGICv3CommonClass;
  268. DECLARE_OBJ_CHECKERS(GICv3State, ARMGICv3CommonClass,
  269. ARM_GICV3_COMMON, TYPE_ARM_GICV3_COMMON)
  270. struct ARMGICv3CommonClass {
  271. /*< private >*/
  272. SysBusDeviceClass parent_class;
  273. /*< public >*/
  274. void (*pre_save)(GICv3State *s);
  275. void (*post_load)(GICv3State *s);
  276. };
  277. void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
  278. const MemoryRegionOps *ops);
  279. /**
  280. * gicv3_class_name
  281. *
  282. * Return name of GICv3 class to use depending on whether KVM acceleration is
  283. * in use. May throw an error if the chosen implementation is not available.
  284. *
  285. * Returns: class name to use
  286. */
  287. const char *gicv3_class_name(void);
  288. #endif