2
0

gicv3_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * ARM GICv3 support - internal interfaces
  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 QEMU_ARM_GICV3_INTERNAL_H
  24. #define QEMU_ARM_GICV3_INTERNAL_H
  25. #include "hw/intc/arm_gicv3_common.h"
  26. /* Distributor registers, as offsets from the distributor base address */
  27. #define GICD_CTLR 0x0000
  28. #define GICD_TYPER 0x0004
  29. #define GICD_IIDR 0x0008
  30. #define GICD_STATUSR 0x0010
  31. #define GICD_SETSPI_NSR 0x0040
  32. #define GICD_CLRSPI_NSR 0x0048
  33. #define GICD_SETSPI_SR 0x0050
  34. #define GICD_CLRSPI_SR 0x0058
  35. #define GICD_SEIR 0x0068
  36. #define GICD_IGROUPR 0x0080
  37. #define GICD_ISENABLER 0x0100
  38. #define GICD_ICENABLER 0x0180
  39. #define GICD_ISPENDR 0x0200
  40. #define GICD_ICPENDR 0x0280
  41. #define GICD_ISACTIVER 0x0300
  42. #define GICD_ICACTIVER 0x0380
  43. #define GICD_IPRIORITYR 0x0400
  44. #define GICD_ITARGETSR 0x0800
  45. #define GICD_ICFGR 0x0C00
  46. #define GICD_IGRPMODR 0x0D00
  47. #define GICD_NSACR 0x0E00
  48. #define GICD_SGIR 0x0F00
  49. #define GICD_CPENDSGIR 0x0F10
  50. #define GICD_SPENDSGIR 0x0F20
  51. #define GICD_IROUTER 0x6000
  52. #define GICD_IDREGS 0xFFD0
  53. /* GICD_CTLR fields */
  54. #define GICD_CTLR_EN_GRP0 (1U << 0)
  55. #define GICD_CTLR_EN_GRP1NS (1U << 1) /* GICv3 5.3.20 */
  56. #define GICD_CTLR_EN_GRP1S (1U << 2)
  57. #define GICD_CTLR_EN_GRP1_ALL (GICD_CTLR_EN_GRP1NS | GICD_CTLR_EN_GRP1S)
  58. /* Bit 4 is ARE if the system doesn't support TrustZone, ARE_S otherwise */
  59. #define GICD_CTLR_ARE (1U << 4)
  60. #define GICD_CTLR_ARE_S (1U << 4)
  61. #define GICD_CTLR_ARE_NS (1U << 5)
  62. #define GICD_CTLR_DS (1U << 6)
  63. #define GICD_CTLR_E1NWF (1U << 7)
  64. #define GICD_CTLR_RWP (1U << 31)
  65. /*
  66. * Redistributor frame offsets from RD_base
  67. */
  68. #define GICR_SGI_OFFSET 0x10000
  69. /*
  70. * Redistributor registers, offsets from RD_base
  71. */
  72. #define GICR_CTLR 0x0000
  73. #define GICR_IIDR 0x0004
  74. #define GICR_TYPER 0x0008
  75. #define GICR_STATUSR 0x0010
  76. #define GICR_WAKER 0x0014
  77. #define GICR_SETLPIR 0x0040
  78. #define GICR_CLRLPIR 0x0048
  79. #define GICR_PROPBASER 0x0070
  80. #define GICR_PENDBASER 0x0078
  81. #define GICR_INVLPIR 0x00A0
  82. #define GICR_INVALLR 0x00B0
  83. #define GICR_SYNCR 0x00C0
  84. #define GICR_IDREGS 0xFFD0
  85. /* SGI and PPI Redistributor registers, offsets from RD_base */
  86. #define GICR_IGROUPR0 (GICR_SGI_OFFSET + 0x0080)
  87. #define GICR_ISENABLER0 (GICR_SGI_OFFSET + 0x0100)
  88. #define GICR_ICENABLER0 (GICR_SGI_OFFSET + 0x0180)
  89. #define GICR_ISPENDR0 (GICR_SGI_OFFSET + 0x0200)
  90. #define GICR_ICPENDR0 (GICR_SGI_OFFSET + 0x0280)
  91. #define GICR_ISACTIVER0 (GICR_SGI_OFFSET + 0x0300)
  92. #define GICR_ICACTIVER0 (GICR_SGI_OFFSET + 0x0380)
  93. #define GICR_IPRIORITYR (GICR_SGI_OFFSET + 0x0400)
  94. #define GICR_ICFGR0 (GICR_SGI_OFFSET + 0x0C00)
  95. #define GICR_ICFGR1 (GICR_SGI_OFFSET + 0x0C04)
  96. #define GICR_IGRPMODR0 (GICR_SGI_OFFSET + 0x0D00)
  97. #define GICR_NSACR (GICR_SGI_OFFSET + 0x0E00)
  98. #define GICR_CTLR_ENABLE_LPIS (1U << 0)
  99. #define GICR_CTLR_RWP (1U << 3)
  100. #define GICR_CTLR_DPG0 (1U << 24)
  101. #define GICR_CTLR_DPG1NS (1U << 25)
  102. #define GICR_CTLR_DPG1S (1U << 26)
  103. #define GICR_CTLR_UWP (1U << 31)
  104. #define GICR_TYPER_PLPIS (1U << 0)
  105. #define GICR_TYPER_VLPIS (1U << 1)
  106. #define GICR_TYPER_DIRECTLPI (1U << 3)
  107. #define GICR_TYPER_LAST (1U << 4)
  108. #define GICR_TYPER_DPGS (1U << 5)
  109. #define GICR_TYPER_PROCNUM (0xFFFFU << 8)
  110. #define GICR_TYPER_COMMONLPIAFF (0x3 << 24)
  111. #define GICR_TYPER_AFFINITYVALUE (0xFFFFFFFFULL << 32)
  112. #define GICR_WAKER_ProcessorSleep (1U << 1)
  113. #define GICR_WAKER_ChildrenAsleep (1U << 2)
  114. #define GICR_PROPBASER_OUTER_CACHEABILITY_MASK (7ULL << 56)
  115. #define GICR_PROPBASER_ADDR_MASK (0xfffffffffULL << 12)
  116. #define GICR_PROPBASER_SHAREABILITY_MASK (3U << 10)
  117. #define GICR_PROPBASER_CACHEABILITY_MASK (7U << 7)
  118. #define GICR_PROPBASER_IDBITS_MASK (0x1f)
  119. #define GICR_PENDBASER_PTZ (1ULL << 62)
  120. #define GICR_PENDBASER_OUTER_CACHEABILITY_MASK (7ULL << 56)
  121. #define GICR_PENDBASER_ADDR_MASK (0xffffffffULL << 16)
  122. #define GICR_PENDBASER_SHAREABILITY_MASK (3U << 10)
  123. #define GICR_PENDBASER_CACHEABILITY_MASK (7U << 7)
  124. #define ICC_CTLR_EL1_CBPR (1U << 0)
  125. #define ICC_CTLR_EL1_EOIMODE (1U << 1)
  126. #define ICC_CTLR_EL1_PMHE (1U << 6)
  127. #define ICC_CTLR_EL1_PRIBITS_SHIFT 8
  128. #define ICC_CTLR_EL1_PRIBITS_MASK (7U << ICC_CTLR_EL1_PRIBITS_SHIFT)
  129. #define ICC_CTLR_EL1_IDBITS_SHIFT 11
  130. #define ICC_CTLR_EL1_SEIS (1U << 14)
  131. #define ICC_CTLR_EL1_A3V (1U << 15)
  132. #define ICC_PMR_PRIORITY_MASK 0xff
  133. #define ICC_BPR_BINARYPOINT_MASK 0x07
  134. #define ICC_IGRPEN_ENABLE 0x01
  135. #define ICC_CTLR_EL3_CBPR_EL1S (1U << 0)
  136. #define ICC_CTLR_EL3_CBPR_EL1NS (1U << 1)
  137. #define ICC_CTLR_EL3_EOIMODE_EL3 (1U << 2)
  138. #define ICC_CTLR_EL3_EOIMODE_EL1S (1U << 3)
  139. #define ICC_CTLR_EL3_EOIMODE_EL1NS (1U << 4)
  140. #define ICC_CTLR_EL3_RM (1U << 5)
  141. #define ICC_CTLR_EL3_PMHE (1U << 6)
  142. #define ICC_CTLR_EL3_PRIBITS_SHIFT 8
  143. #define ICC_CTLR_EL3_IDBITS_SHIFT 11
  144. #define ICC_CTLR_EL3_SEIS (1U << 14)
  145. #define ICC_CTLR_EL3_A3V (1U << 15)
  146. #define ICC_CTLR_EL3_NDS (1U << 17)
  147. #define ICH_VMCR_EL2_VENG0_SHIFT 0
  148. #define ICH_VMCR_EL2_VENG0 (1U << ICH_VMCR_EL2_VENG0_SHIFT)
  149. #define ICH_VMCR_EL2_VENG1_SHIFT 1
  150. #define ICH_VMCR_EL2_VENG1 (1U << ICH_VMCR_EL2_VENG1_SHIFT)
  151. #define ICH_VMCR_EL2_VACKCTL (1U << 2)
  152. #define ICH_VMCR_EL2_VFIQEN (1U << 3)
  153. #define ICH_VMCR_EL2_VCBPR_SHIFT 4
  154. #define ICH_VMCR_EL2_VCBPR (1U << ICH_VMCR_EL2_VCBPR_SHIFT)
  155. #define ICH_VMCR_EL2_VEOIM_SHIFT 9
  156. #define ICH_VMCR_EL2_VEOIM (1U << ICH_VMCR_EL2_VEOIM_SHIFT)
  157. #define ICH_VMCR_EL2_VBPR1_SHIFT 18
  158. #define ICH_VMCR_EL2_VBPR1_LENGTH 3
  159. #define ICH_VMCR_EL2_VBPR1_MASK (0x7U << ICH_VMCR_EL2_VBPR1_SHIFT)
  160. #define ICH_VMCR_EL2_VBPR0_SHIFT 21
  161. #define ICH_VMCR_EL2_VBPR0_LENGTH 3
  162. #define ICH_VMCR_EL2_VBPR0_MASK (0x7U << ICH_VMCR_EL2_VBPR0_SHIFT)
  163. #define ICH_VMCR_EL2_VPMR_SHIFT 24
  164. #define ICH_VMCR_EL2_VPMR_LENGTH 8
  165. #define ICH_VMCR_EL2_VPMR_MASK (0xffU << ICH_VMCR_EL2_VPMR_SHIFT)
  166. #define ICH_HCR_EL2_EN (1U << 0)
  167. #define ICH_HCR_EL2_UIE (1U << 1)
  168. #define ICH_HCR_EL2_LRENPIE (1U << 2)
  169. #define ICH_HCR_EL2_NPIE (1U << 3)
  170. #define ICH_HCR_EL2_VGRP0EIE (1U << 4)
  171. #define ICH_HCR_EL2_VGRP0DIE (1U << 5)
  172. #define ICH_HCR_EL2_VGRP1EIE (1U << 6)
  173. #define ICH_HCR_EL2_VGRP1DIE (1U << 7)
  174. #define ICH_HCR_EL2_TC (1U << 10)
  175. #define ICH_HCR_EL2_TALL0 (1U << 11)
  176. #define ICH_HCR_EL2_TALL1 (1U << 12)
  177. #define ICH_HCR_EL2_TSEI (1U << 13)
  178. #define ICH_HCR_EL2_TDIR (1U << 14)
  179. #define ICH_HCR_EL2_EOICOUNT_SHIFT 27
  180. #define ICH_HCR_EL2_EOICOUNT_LENGTH 5
  181. #define ICH_HCR_EL2_EOICOUNT_MASK (0x1fU << ICH_HCR_EL2_EOICOUNT_SHIFT)
  182. #define ICH_LR_EL2_VINTID_SHIFT 0
  183. #define ICH_LR_EL2_VINTID_LENGTH 32
  184. #define ICH_LR_EL2_VINTID_MASK (0xffffffffULL << ICH_LR_EL2_VINTID_SHIFT)
  185. #define ICH_LR_EL2_PINTID_SHIFT 32
  186. #define ICH_LR_EL2_PINTID_LENGTH 10
  187. #define ICH_LR_EL2_PINTID_MASK (0x3ffULL << ICH_LR_EL2_PINTID_SHIFT)
  188. /* Note that EOI shares with the top bit of the pINTID field */
  189. #define ICH_LR_EL2_EOI (1ULL << 41)
  190. #define ICH_LR_EL2_PRIORITY_SHIFT 48
  191. #define ICH_LR_EL2_PRIORITY_LENGTH 8
  192. #define ICH_LR_EL2_PRIORITY_MASK (0xffULL << ICH_LR_EL2_PRIORITY_SHIFT)
  193. #define ICH_LR_EL2_GROUP (1ULL << 60)
  194. #define ICH_LR_EL2_HW (1ULL << 61)
  195. #define ICH_LR_EL2_STATE_SHIFT 62
  196. #define ICH_LR_EL2_STATE_LENGTH 2
  197. #define ICH_LR_EL2_STATE_MASK (3ULL << ICH_LR_EL2_STATE_SHIFT)
  198. /* values for the state field: */
  199. #define ICH_LR_EL2_STATE_INVALID 0
  200. #define ICH_LR_EL2_STATE_PENDING 1
  201. #define ICH_LR_EL2_STATE_ACTIVE 2
  202. #define ICH_LR_EL2_STATE_ACTIVE_PENDING 3
  203. #define ICH_LR_EL2_STATE_PENDING_BIT (1ULL << ICH_LR_EL2_STATE_SHIFT)
  204. #define ICH_LR_EL2_STATE_ACTIVE_BIT (2ULL << ICH_LR_EL2_STATE_SHIFT)
  205. #define ICH_MISR_EL2_EOI (1U << 0)
  206. #define ICH_MISR_EL2_U (1U << 1)
  207. #define ICH_MISR_EL2_LRENP (1U << 2)
  208. #define ICH_MISR_EL2_NP (1U << 3)
  209. #define ICH_MISR_EL2_VGRP0E (1U << 4)
  210. #define ICH_MISR_EL2_VGRP0D (1U << 5)
  211. #define ICH_MISR_EL2_VGRP1E (1U << 6)
  212. #define ICH_MISR_EL2_VGRP1D (1U << 7)
  213. #define ICH_VTR_EL2_LISTREGS_SHIFT 0
  214. #define ICH_VTR_EL2_TDS (1U << 19)
  215. #define ICH_VTR_EL2_NV4 (1U << 20)
  216. #define ICH_VTR_EL2_A3V (1U << 21)
  217. #define ICH_VTR_EL2_SEIS (1U << 22)
  218. #define ICH_VTR_EL2_IDBITS_SHIFT 23
  219. #define ICH_VTR_EL2_PREBITS_SHIFT 26
  220. #define ICH_VTR_EL2_PRIBITS_SHIFT 29
  221. /* Special interrupt IDs */
  222. #define INTID_SECURE 1020
  223. #define INTID_NONSECURE 1021
  224. #define INTID_SPURIOUS 1023
  225. /* Functions internal to the emulated GICv3 */
  226. /**
  227. * gicv3_redist_update:
  228. * @cs: GICv3CPUState for this redistributor
  229. *
  230. * Recalculate the highest priority pending interrupt after a
  231. * change to redistributor state, and inform the CPU accordingly.
  232. */
  233. void gicv3_redist_update(GICv3CPUState *cs);
  234. /**
  235. * gicv3_update:
  236. * @s: GICv3State
  237. * @start: first interrupt whose state changed
  238. * @len: length of the range of interrupts whose state changed
  239. *
  240. * Recalculate the highest priority pending interrupts after a
  241. * change to the distributor state affecting @len interrupts
  242. * starting at @start, and inform the CPUs accordingly.
  243. */
  244. void gicv3_update(GICv3State *s, int start, int len);
  245. /**
  246. * gicv3_full_update_noirqset:
  247. * @s: GICv3State
  248. *
  249. * Recalculate the cached information about highest priority
  250. * pending interrupts, but don't inform the CPUs. This should be
  251. * called after an incoming migration has loaded new state.
  252. */
  253. void gicv3_full_update_noirqset(GICv3State *s);
  254. /**
  255. * gicv3_full_update:
  256. * @s: GICv3State
  257. *
  258. * Recalculate the highest priority pending interrupts after
  259. * a change that could affect the status of all interrupts,
  260. * and inform the CPUs accordingly.
  261. */
  262. void gicv3_full_update(GICv3State *s);
  263. MemTxResult gicv3_dist_read(void *opaque, hwaddr offset, uint64_t *data,
  264. unsigned size, MemTxAttrs attrs);
  265. MemTxResult gicv3_dist_write(void *opaque, hwaddr addr, uint64_t data,
  266. unsigned size, MemTxAttrs attrs);
  267. MemTxResult gicv3_redist_read(void *opaque, hwaddr offset, uint64_t *data,
  268. unsigned size, MemTxAttrs attrs);
  269. MemTxResult gicv3_redist_write(void *opaque, hwaddr offset, uint64_t data,
  270. unsigned size, MemTxAttrs attrs);
  271. void gicv3_dist_set_irq(GICv3State *s, int irq, int level);
  272. void gicv3_redist_set_irq(GICv3CPUState *cs, int irq, int level);
  273. void gicv3_redist_send_sgi(GICv3CPUState *cs, int grp, int irq, bool ns);
  274. void gicv3_init_cpuif(GICv3State *s);
  275. /**
  276. * gicv3_cpuif_update:
  277. * @cs: GICv3CPUState for the CPU to update
  278. *
  279. * Recalculate whether to assert the IRQ or FIQ lines after a change
  280. * to the current highest priority pending interrupt, the CPU's
  281. * current running priority or the CPU's current exception level or
  282. * security state.
  283. */
  284. void gicv3_cpuif_update(GICv3CPUState *cs);
  285. static inline uint32_t gicv3_iidr(void)
  286. {
  287. /* Return the Implementer Identification Register value
  288. * for the emulated GICv3, as reported in GICD_IIDR and GICR_IIDR.
  289. *
  290. * We claim to be an ARM r0p0 with a zero ProductID.
  291. * This is the same as an r0p0 GIC-500.
  292. */
  293. return 0x43b;
  294. }
  295. static inline uint32_t gicv3_idreg(int regoffset)
  296. {
  297. /* Return the value of the CoreSight ID register at the specified
  298. * offset from the first ID register (as found in the distributor
  299. * and redistributor register banks).
  300. * These values indicate an ARM implementation of a GICv3.
  301. */
  302. static const uint8_t gicd_ids[] = {
  303. 0x44, 0x00, 0x00, 0x00, 0x92, 0xB4, 0x3B, 0x00, 0x0D, 0xF0, 0x05, 0xB1
  304. };
  305. return gicd_ids[regoffset / 4];
  306. }
  307. /**
  308. * gicv3_irq_group:
  309. *
  310. * Return the group which this interrupt is configured as (GICV3_G0,
  311. * GICV3_G1 or GICV3_G1NS).
  312. */
  313. static inline int gicv3_irq_group(GICv3State *s, GICv3CPUState *cs, int irq)
  314. {
  315. bool grpbit, grpmodbit;
  316. if (irq < GIC_INTERNAL) {
  317. grpbit = extract32(cs->gicr_igroupr0, irq, 1);
  318. grpmodbit = extract32(cs->gicr_igrpmodr0, irq, 1);
  319. } else {
  320. grpbit = gicv3_gicd_group_test(s, irq);
  321. grpmodbit = gicv3_gicd_grpmod_test(s, irq);
  322. }
  323. if (grpbit) {
  324. return GICV3_G1NS;
  325. }
  326. if (s->gicd_ctlr & GICD_CTLR_DS) {
  327. return GICV3_G0;
  328. }
  329. return grpmodbit ? GICV3_G1 : GICV3_G0;
  330. }
  331. /**
  332. * gicv3_redist_affid:
  333. *
  334. * Return the 32-bit affinity ID of the CPU connected to this redistributor
  335. */
  336. static inline uint32_t gicv3_redist_affid(GICv3CPUState *cs)
  337. {
  338. return cs->gicr_typer >> 32;
  339. }
  340. /**
  341. * gicv3_cache_target_cpustate:
  342. *
  343. * Update the cached CPU state corresponding to the target for this interrupt
  344. * (which is kept in s->gicd_irouter_target[]).
  345. */
  346. static inline void gicv3_cache_target_cpustate(GICv3State *s, int irq)
  347. {
  348. GICv3CPUState *cs = NULL;
  349. int i;
  350. uint32_t tgtaff = extract64(s->gicd_irouter[irq], 0, 24) |
  351. extract64(s->gicd_irouter[irq], 32, 8) << 24;
  352. for (i = 0; i < s->num_cpu; i++) {
  353. if (s->cpu[i].gicr_typer >> 32 == tgtaff) {
  354. cs = &s->cpu[i];
  355. break;
  356. }
  357. }
  358. s->gicd_irouter_target[irq] = cs;
  359. }
  360. /**
  361. * gicv3_cache_all_target_cpustates:
  362. *
  363. * Populate the entire cache of CPU state pointers for interrupt targets
  364. * (eg after inbound migration or CPU reset)
  365. */
  366. static inline void gicv3_cache_all_target_cpustates(GICv3State *s)
  367. {
  368. int irq;
  369. for (irq = GIC_INTERNAL; irq < GICV3_MAXIRQ; irq++) {
  370. gicv3_cache_target_cpustate(s, irq);
  371. }
  372. }
  373. void gicv3_set_gicv3state(CPUState *cpu, GICv3CPUState *s);
  374. #endif /* QEMU_ARM_GICV3_INTERNAL_H */