arm_gicv3_kvm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * ARM Generic Interrupt Controller using KVM in-kernel support
  3. *
  4. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  5. * Written by Pavel Fedin
  6. * Based on vGICv2 code by Peter Maydell
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qapi/error.h"
  23. #include "hw/intc/arm_gicv3_common.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/module.h"
  26. #include "system/kvm.h"
  27. #include "system/runstate.h"
  28. #include "kvm_arm.h"
  29. #include "gicv3_internal.h"
  30. #include "vgic_common.h"
  31. #include "migration/blocker.h"
  32. #include "qom/object.h"
  33. #include "target/arm/cpregs.h"
  34. #ifdef DEBUG_GICV3_KVM
  35. #define DPRINTF(fmt, ...) \
  36. do { fprintf(stderr, "kvm_gicv3: " fmt, ## __VA_ARGS__); } while (0)
  37. #else
  38. #define DPRINTF(fmt, ...) \
  39. do { } while (0)
  40. #endif
  41. #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
  42. typedef struct KVMARMGICv3Class KVMARMGICv3Class;
  43. /* This is reusing the GICv3State typedef from ARM_GICV3_ITS_COMMON */
  44. DECLARE_OBJ_CHECKERS(GICv3State, KVMARMGICv3Class,
  45. KVM_ARM_GICV3, TYPE_KVM_ARM_GICV3)
  46. #define KVM_DEV_ARM_VGIC_SYSREG(op0, op1, crn, crm, op2) \
  47. (ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
  48. ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
  49. ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
  50. ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
  51. ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
  52. #define ICC_PMR_EL1 \
  53. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 4, 6, 0)
  54. #define ICC_BPR0_EL1 \
  55. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 3)
  56. #define ICC_AP0R_EL1(n) \
  57. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 4 | n)
  58. #define ICC_AP1R_EL1(n) \
  59. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 9, n)
  60. #define ICC_BPR1_EL1 \
  61. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 3)
  62. #define ICC_CTLR_EL1 \
  63. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 4)
  64. #define ICC_SRE_EL1 \
  65. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 5)
  66. #define ICC_IGRPEN0_EL1 \
  67. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 6)
  68. #define ICC_IGRPEN1_EL1 \
  69. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 7)
  70. struct KVMARMGICv3Class {
  71. ARMGICv3CommonClass parent_class;
  72. DeviceRealize parent_realize;
  73. ResettablePhases parent_phases;
  74. };
  75. static void kvm_arm_gicv3_set_irq(void *opaque, int irq, int level)
  76. {
  77. GICv3State *s = (GICv3State *)opaque;
  78. kvm_arm_gic_set_irq(s->num_irq, irq, level);
  79. }
  80. #define KVM_VGIC_ATTR(reg, typer) \
  81. ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (reg))
  82. static inline void kvm_gicd_access(GICv3State *s, int offset,
  83. uint32_t *val, bool write)
  84. {
  85. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
  86. KVM_VGIC_ATTR(offset, 0),
  87. val, write, &error_abort);
  88. }
  89. static inline void kvm_gicr_access(GICv3State *s, int offset, int cpu,
  90. uint32_t *val, bool write)
  91. {
  92. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
  93. KVM_VGIC_ATTR(offset, s->cpu[cpu].gicr_typer),
  94. val, write, &error_abort);
  95. }
  96. static inline void kvm_gicc_access(GICv3State *s, uint64_t reg, int cpu,
  97. uint64_t *val, bool write)
  98. {
  99. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
  100. KVM_VGIC_ATTR(reg, s->cpu[cpu].gicr_typer),
  101. val, write, &error_abort);
  102. }
  103. static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu,
  104. uint32_t *val, bool write)
  105. {
  106. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
  107. KVM_VGIC_ATTR(irq, s->cpu[cpu].gicr_typer) |
  108. (VGIC_LEVEL_INFO_LINE_LEVEL <<
  109. KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT),
  110. val, write, &error_abort);
  111. }
  112. /* Loop through each distributor IRQ related register; since bits
  113. * corresponding to SPIs and PPIs are RAZ/WI when affinity routing
  114. * is enabled, we skip those.
  115. */
  116. #define for_each_dist_irq_reg(_irq, _max, _field_width) \
  117. for (_irq = GIC_INTERNAL; _irq < _max; _irq += (32 / _field_width))
  118. static void kvm_dist_get_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
  119. {
  120. uint32_t reg, *field;
  121. int irq;
  122. /* For the KVM GICv3, affinity routing is always enabled, and the first 8
  123. * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
  124. * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
  125. * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
  126. * offset.
  127. */
  128. field = (uint32_t *)(bmp + GIC_INTERNAL);
  129. offset += (GIC_INTERNAL * 8) / 8;
  130. for_each_dist_irq_reg(irq, s->num_irq, 8) {
  131. kvm_gicd_access(s, offset, &reg, false);
  132. *field = reg;
  133. offset += 4;
  134. field++;
  135. }
  136. }
  137. static void kvm_dist_put_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
  138. {
  139. uint32_t reg, *field;
  140. int irq;
  141. /* For the KVM GICv3, affinity routing is always enabled, and the first 8
  142. * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
  143. * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
  144. * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
  145. * offset.
  146. */
  147. field = (uint32_t *)(bmp + GIC_INTERNAL);
  148. offset += (GIC_INTERNAL * 8) / 8;
  149. for_each_dist_irq_reg(irq, s->num_irq, 8) {
  150. reg = *field;
  151. kvm_gicd_access(s, offset, &reg, true);
  152. offset += 4;
  153. field++;
  154. }
  155. }
  156. static void kvm_dist_get_edge_trigger(GICv3State *s, uint32_t offset,
  157. uint32_t *bmp)
  158. {
  159. uint32_t reg;
  160. int irq;
  161. /* For the KVM GICv3, affinity routing is always enabled, and the first 2
  162. * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
  163. * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
  164. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  165. * This matches the for_each_dist_irq_reg() macro which also skips the
  166. * first GIC_INTERNAL irqs.
  167. */
  168. offset += (GIC_INTERNAL * 2) / 8;
  169. for_each_dist_irq_reg(irq, s->num_irq, 2) {
  170. kvm_gicd_access(s, offset, &reg, false);
  171. reg = half_unshuffle32(reg >> 1);
  172. if (irq % 32 != 0) {
  173. reg = (reg << 16);
  174. }
  175. *gic_bmp_ptr32(bmp, irq) |= reg;
  176. offset += 4;
  177. }
  178. }
  179. static void kvm_dist_put_edge_trigger(GICv3State *s, uint32_t offset,
  180. uint32_t *bmp)
  181. {
  182. uint32_t reg;
  183. int irq;
  184. /* For the KVM GICv3, affinity routing is always enabled, and the first 2
  185. * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
  186. * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
  187. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  188. * This matches the for_each_dist_irq_reg() macro which also skips the
  189. * first GIC_INTERNAL irqs.
  190. */
  191. offset += (GIC_INTERNAL * 2) / 8;
  192. for_each_dist_irq_reg(irq, s->num_irq, 2) {
  193. reg = *gic_bmp_ptr32(bmp, irq);
  194. if (irq % 32 != 0) {
  195. reg = (reg & 0xffff0000) >> 16;
  196. } else {
  197. reg = reg & 0xffff;
  198. }
  199. reg = half_shuffle32(reg) << 1;
  200. kvm_gicd_access(s, offset, &reg, true);
  201. offset += 4;
  202. }
  203. }
  204. static void kvm_gic_get_line_level_bmp(GICv3State *s, uint32_t *bmp)
  205. {
  206. uint32_t reg;
  207. int irq;
  208. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  209. kvm_gic_line_level_access(s, irq, 0, &reg, false);
  210. *gic_bmp_ptr32(bmp, irq) = reg;
  211. }
  212. }
  213. static void kvm_gic_put_line_level_bmp(GICv3State *s, uint32_t *bmp)
  214. {
  215. uint32_t reg;
  216. int irq;
  217. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  218. reg = *gic_bmp_ptr32(bmp, irq);
  219. kvm_gic_line_level_access(s, irq, 0, &reg, true);
  220. }
  221. }
  222. /* Read a bitmap register group from the kernel VGIC. */
  223. static void kvm_dist_getbmp(GICv3State *s, uint32_t offset, uint32_t *bmp)
  224. {
  225. uint32_t reg;
  226. int irq;
  227. /* For the KVM GICv3, affinity routing is always enabled, and the
  228. * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
  229. * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
  230. * functionality is replaced by the GICR registers. It doesn't need to sync
  231. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  232. * This matches the for_each_dist_irq_reg() macro which also skips the
  233. * first GIC_INTERNAL irqs.
  234. */
  235. offset += (GIC_INTERNAL * 1) / 8;
  236. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  237. kvm_gicd_access(s, offset, &reg, false);
  238. *gic_bmp_ptr32(bmp, irq) = reg;
  239. offset += 4;
  240. }
  241. }
  242. static void kvm_dist_putbmp(GICv3State *s, uint32_t offset,
  243. uint32_t clroffset, uint32_t *bmp)
  244. {
  245. uint32_t reg;
  246. int irq;
  247. /* For the KVM GICv3, affinity routing is always enabled, and the
  248. * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
  249. * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
  250. * functionality is replaced by the GICR registers. It doesn't need to sync
  251. * them. So it should increase the offset and clroffset to skip GIC_INTERNAL
  252. * irqs. This matches the for_each_dist_irq_reg() macro which also skips the
  253. * first GIC_INTERNAL irqs.
  254. */
  255. offset += (GIC_INTERNAL * 1) / 8;
  256. if (clroffset != 0) {
  257. clroffset += (GIC_INTERNAL * 1) / 8;
  258. }
  259. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  260. /* If this bitmap is a set/clear register pair, first write to the
  261. * clear-reg to clear all bits before using the set-reg to write
  262. * the 1 bits.
  263. */
  264. if (clroffset != 0) {
  265. reg = 0;
  266. kvm_gicd_access(s, clroffset, &reg, true);
  267. clroffset += 4;
  268. }
  269. reg = *gic_bmp_ptr32(bmp, irq);
  270. kvm_gicd_access(s, offset, &reg, true);
  271. offset += 4;
  272. }
  273. }
  274. static void kvm_arm_gicv3_check(GICv3State *s)
  275. {
  276. uint32_t reg;
  277. uint32_t num_irq;
  278. /* Sanity checking s->num_irq */
  279. kvm_gicd_access(s, GICD_TYPER, &reg, false);
  280. num_irq = ((reg & 0x1f) + 1) * 32;
  281. if (num_irq < s->num_irq) {
  282. error_report("Model requests %u IRQs, but kernel supports max %u",
  283. s->num_irq, num_irq);
  284. abort();
  285. }
  286. }
  287. static void kvm_arm_gicv3_put(GICv3State *s)
  288. {
  289. uint32_t regl, regh, reg;
  290. uint64_t reg64, redist_typer;
  291. int ncpu, i;
  292. kvm_arm_gicv3_check(s);
  293. kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
  294. kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
  295. redist_typer = ((uint64_t)regh << 32) | regl;
  296. reg = s->gicd_ctlr;
  297. kvm_gicd_access(s, GICD_CTLR, &reg, true);
  298. if (redist_typer & GICR_TYPER_PLPIS) {
  299. /*
  300. * Restore base addresses before LPIs are potentially enabled by
  301. * GICR_CTLR write
  302. */
  303. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  304. GICv3CPUState *c = &s->cpu[ncpu];
  305. reg64 = c->gicr_propbaser;
  306. regl = (uint32_t)reg64;
  307. kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, true);
  308. regh = (uint32_t)(reg64 >> 32);
  309. kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, true);
  310. reg64 = c->gicr_pendbaser;
  311. regl = (uint32_t)reg64;
  312. kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, true);
  313. regh = (uint32_t)(reg64 >> 32);
  314. kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, true);
  315. }
  316. }
  317. /* Redistributor state (one per CPU) */
  318. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  319. GICv3CPUState *c = &s->cpu[ncpu];
  320. reg = c->gicr_ctlr;
  321. kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, true);
  322. reg = c->gicr_statusr[GICV3_NS];
  323. kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, true);
  324. reg = c->gicr_waker;
  325. kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, true);
  326. reg = c->gicr_igroupr0;
  327. kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, true);
  328. reg = ~0;
  329. kvm_gicr_access(s, GICR_ICENABLER0, ncpu, &reg, true);
  330. reg = c->gicr_ienabler0;
  331. kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, true);
  332. /* Restore config before pending so we treat level/edge correctly */
  333. reg = half_shuffle32(c->edge_trigger >> 16) << 1;
  334. kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, true);
  335. reg = c->level;
  336. kvm_gic_line_level_access(s, 0, ncpu, &reg, true);
  337. reg = ~0;
  338. kvm_gicr_access(s, GICR_ICPENDR0, ncpu, &reg, true);
  339. reg = c->gicr_ipendr0;
  340. kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, true);
  341. reg = ~0;
  342. kvm_gicr_access(s, GICR_ICACTIVER0, ncpu, &reg, true);
  343. reg = c->gicr_iactiver0;
  344. kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, true);
  345. for (i = 0; i < GIC_INTERNAL; i += 4) {
  346. reg = c->gicr_ipriorityr[i] |
  347. (c->gicr_ipriorityr[i + 1] << 8) |
  348. (c->gicr_ipriorityr[i + 2] << 16) |
  349. (c->gicr_ipriorityr[i + 3] << 24);
  350. kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, true);
  351. }
  352. }
  353. /* Distributor state (shared between all CPUs */
  354. reg = s->gicd_statusr[GICV3_NS];
  355. kvm_gicd_access(s, GICD_STATUSR, &reg, true);
  356. /* s->enable bitmap -> GICD_ISENABLERn */
  357. kvm_dist_putbmp(s, GICD_ISENABLER, GICD_ICENABLER, s->enabled);
  358. /* s->group bitmap -> GICD_IGROUPRn */
  359. kvm_dist_putbmp(s, GICD_IGROUPR, 0, s->group);
  360. /* Restore targets before pending to ensure the pending state is set on
  361. * the appropriate CPU interfaces in the kernel
  362. */
  363. /* s->gicd_irouter[irq] -> GICD_IROUTERn
  364. * We can't use kvm_dist_put() here because the registers are 64-bit
  365. */
  366. for (i = GIC_INTERNAL; i < s->num_irq; i++) {
  367. uint32_t offset;
  368. offset = GICD_IROUTER + (sizeof(uint32_t) * i);
  369. reg = (uint32_t)s->gicd_irouter[i];
  370. kvm_gicd_access(s, offset, &reg, true);
  371. offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
  372. reg = (uint32_t)(s->gicd_irouter[i] >> 32);
  373. kvm_gicd_access(s, offset, &reg, true);
  374. }
  375. /* s->trigger bitmap -> GICD_ICFGRn
  376. * (restore configuration registers before pending IRQs so we treat
  377. * level/edge correctly)
  378. */
  379. kvm_dist_put_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
  380. /* s->level bitmap -> line_level */
  381. kvm_gic_put_line_level_bmp(s, s->level);
  382. /* s->pending bitmap -> GICD_ISPENDRn */
  383. kvm_dist_putbmp(s, GICD_ISPENDR, GICD_ICPENDR, s->pending);
  384. /* s->active bitmap -> GICD_ISACTIVERn */
  385. kvm_dist_putbmp(s, GICD_ISACTIVER, GICD_ICACTIVER, s->active);
  386. /* s->gicd_ipriority[] -> GICD_IPRIORITYRn */
  387. kvm_dist_put_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
  388. /* CPU Interface state (one per CPU) */
  389. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  390. GICv3CPUState *c = &s->cpu[ncpu];
  391. int num_pri_bits;
  392. kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, true);
  393. kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
  394. &c->icc_ctlr_el1[GICV3_NS], true);
  395. kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
  396. &c->icc_igrpen[GICV3_G0], true);
  397. kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
  398. &c->icc_igrpen[GICV3_G1NS], true);
  399. kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, true);
  400. kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], true);
  401. kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], true);
  402. num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
  403. ICC_CTLR_EL1_PRIBITS_MASK) >>
  404. ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
  405. switch (num_pri_bits) {
  406. case 7:
  407. reg64 = c->icc_apr[GICV3_G0][3];
  408. kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, true);
  409. reg64 = c->icc_apr[GICV3_G0][2];
  410. kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, true);
  411. /* fall through */
  412. case 6:
  413. reg64 = c->icc_apr[GICV3_G0][1];
  414. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
  415. /* fall through */
  416. default:
  417. reg64 = c->icc_apr[GICV3_G0][0];
  418. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
  419. }
  420. switch (num_pri_bits) {
  421. case 7:
  422. reg64 = c->icc_apr[GICV3_G1NS][3];
  423. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
  424. reg64 = c->icc_apr[GICV3_G1NS][2];
  425. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
  426. /* fall through */
  427. case 6:
  428. reg64 = c->icc_apr[GICV3_G1NS][1];
  429. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
  430. /* fall through */
  431. default:
  432. reg64 = c->icc_apr[GICV3_G1NS][0];
  433. kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, true);
  434. }
  435. }
  436. }
  437. static void kvm_arm_gicv3_get(GICv3State *s)
  438. {
  439. uint32_t regl, regh, reg;
  440. uint64_t reg64, redist_typer;
  441. int ncpu, i;
  442. kvm_arm_gicv3_check(s);
  443. kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
  444. kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
  445. redist_typer = ((uint64_t)regh << 32) | regl;
  446. kvm_gicd_access(s, GICD_CTLR, &reg, false);
  447. s->gicd_ctlr = reg;
  448. /* Redistributor state (one per CPU) */
  449. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  450. GICv3CPUState *c = &s->cpu[ncpu];
  451. kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, false);
  452. c->gicr_ctlr = reg;
  453. kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, false);
  454. c->gicr_statusr[GICV3_NS] = reg;
  455. kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, false);
  456. c->gicr_waker = reg;
  457. kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, false);
  458. c->gicr_igroupr0 = reg;
  459. kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, false);
  460. c->gicr_ienabler0 = reg;
  461. kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, false);
  462. c->edge_trigger = half_unshuffle32(reg >> 1) << 16;
  463. kvm_gic_line_level_access(s, 0, ncpu, &reg, false);
  464. c->level = reg;
  465. kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, false);
  466. c->gicr_ipendr0 = reg;
  467. kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, false);
  468. c->gicr_iactiver0 = reg;
  469. for (i = 0; i < GIC_INTERNAL; i += 4) {
  470. kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, false);
  471. c->gicr_ipriorityr[i] = extract32(reg, 0, 8);
  472. c->gicr_ipriorityr[i + 1] = extract32(reg, 8, 8);
  473. c->gicr_ipriorityr[i + 2] = extract32(reg, 16, 8);
  474. c->gicr_ipriorityr[i + 3] = extract32(reg, 24, 8);
  475. }
  476. }
  477. if (redist_typer & GICR_TYPER_PLPIS) {
  478. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  479. GICv3CPUState *c = &s->cpu[ncpu];
  480. kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, false);
  481. kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, false);
  482. c->gicr_propbaser = ((uint64_t)regh << 32) | regl;
  483. kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, false);
  484. kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, false);
  485. c->gicr_pendbaser = ((uint64_t)regh << 32) | regl;
  486. }
  487. }
  488. /* Distributor state (shared between all CPUs */
  489. kvm_gicd_access(s, GICD_STATUSR, &reg, false);
  490. s->gicd_statusr[GICV3_NS] = reg;
  491. /* GICD_IGROUPRn -> s->group bitmap */
  492. kvm_dist_getbmp(s, GICD_IGROUPR, s->group);
  493. /* GICD_ISENABLERn -> s->enabled bitmap */
  494. kvm_dist_getbmp(s, GICD_ISENABLER, s->enabled);
  495. /* Line level of irq */
  496. kvm_gic_get_line_level_bmp(s, s->level);
  497. /* GICD_ISPENDRn -> s->pending bitmap */
  498. kvm_dist_getbmp(s, GICD_ISPENDR, s->pending);
  499. /* GICD_ISACTIVERn -> s->active bitmap */
  500. kvm_dist_getbmp(s, GICD_ISACTIVER, s->active);
  501. /* GICD_ICFGRn -> s->trigger bitmap */
  502. kvm_dist_get_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
  503. /* GICD_IPRIORITYRn -> s->gicd_ipriority[] */
  504. kvm_dist_get_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
  505. /* GICD_IROUTERn -> s->gicd_irouter[irq] */
  506. for (i = GIC_INTERNAL; i < s->num_irq; i++) {
  507. uint32_t offset;
  508. offset = GICD_IROUTER + (sizeof(uint32_t) * i);
  509. kvm_gicd_access(s, offset, &regl, false);
  510. offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
  511. kvm_gicd_access(s, offset, &regh, false);
  512. s->gicd_irouter[i] = ((uint64_t)regh << 32) | regl;
  513. }
  514. /*****************************************************************
  515. * CPU Interface(s) State
  516. */
  517. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  518. GICv3CPUState *c = &s->cpu[ncpu];
  519. int num_pri_bits;
  520. kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, false);
  521. kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
  522. &c->icc_ctlr_el1[GICV3_NS], false);
  523. kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
  524. &c->icc_igrpen[GICV3_G0], false);
  525. kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
  526. &c->icc_igrpen[GICV3_G1NS], false);
  527. kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, false);
  528. kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], false);
  529. kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], false);
  530. num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
  531. ICC_CTLR_EL1_PRIBITS_MASK) >>
  532. ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
  533. switch (num_pri_bits) {
  534. case 7:
  535. kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, false);
  536. c->icc_apr[GICV3_G0][3] = reg64;
  537. kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, false);
  538. c->icc_apr[GICV3_G0][2] = reg64;
  539. /* fall through */
  540. case 6:
  541. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
  542. c->icc_apr[GICV3_G0][1] = reg64;
  543. /* fall through */
  544. default:
  545. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
  546. c->icc_apr[GICV3_G0][0] = reg64;
  547. }
  548. switch (num_pri_bits) {
  549. case 7:
  550. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
  551. c->icc_apr[GICV3_G1NS][3] = reg64;
  552. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
  553. c->icc_apr[GICV3_G1NS][2] = reg64;
  554. /* fall through */
  555. case 6:
  556. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
  557. c->icc_apr[GICV3_G1NS][1] = reg64;
  558. /* fall through */
  559. default:
  560. kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
  561. c->icc_apr[GICV3_G1NS][0] = reg64;
  562. }
  563. }
  564. }
  565. static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  566. {
  567. GICv3State *s;
  568. GICv3CPUState *c;
  569. c = (GICv3CPUState *)env->gicv3state;
  570. s = c->gic;
  571. c->icc_pmr_el1 = 0;
  572. /*
  573. * Architecturally the reset value of the ICC_BPR registers
  574. * is UNKNOWN. We set them all to 0 here; when the kernel
  575. * uses these values to program the ICH_VMCR_EL2 fields that
  576. * determine the guest-visible ICC_BPR register values, the
  577. * hardware's "writing a value less than the minimum sets
  578. * the field to the minimum value" behaviour will result in
  579. * them effectively resetting to the correct minimum value
  580. * for the host GIC.
  581. */
  582. c->icc_bpr[GICV3_G0] = 0;
  583. c->icc_bpr[GICV3_G1] = 0;
  584. c->icc_bpr[GICV3_G1NS] = 0;
  585. c->icc_sre_el1 = 0x7;
  586. memset(c->icc_apr, 0, sizeof(c->icc_apr));
  587. memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
  588. if (s->migration_blocker) {
  589. return;
  590. }
  591. /* Initialize to actual HW supported configuration */
  592. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
  593. KVM_VGIC_ATTR(ICC_CTLR_EL1, c->gicr_typer),
  594. &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
  595. c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
  596. }
  597. static void kvm_arm_gicv3_reset_hold(Object *obj, ResetType type)
  598. {
  599. GICv3State *s = ARM_GICV3_COMMON(obj);
  600. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  601. DPRINTF("Reset\n");
  602. if (kgc->parent_phases.hold) {
  603. kgc->parent_phases.hold(obj, type);
  604. }
  605. if (s->migration_blocker) {
  606. DPRINTF("Cannot put kernel gic state, no kernel interface\n");
  607. return;
  608. }
  609. kvm_arm_gicv3_put(s);
  610. }
  611. /*
  612. * CPU interface registers of GIC needs to be reset on CPU reset.
  613. * For the calling arm_gicv3_icc_reset() on CPU reset, we register
  614. * below ARMCPRegInfo. As we reset the whole cpu interface under single
  615. * register reset, we define only one register of CPU interface instead
  616. * of defining all the registers.
  617. */
  618. static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
  619. { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
  620. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
  621. /*
  622. * If ARM_CP_NOP is used, resetfn is not called,
  623. * So ARM_CP_NO_RAW is appropriate type.
  624. */
  625. .type = ARM_CP_NO_RAW,
  626. .access = PL1_RW,
  627. .readfn = arm_cp_read_zero,
  628. .writefn = arm_cp_write_ignore,
  629. /*
  630. * We hang the whole cpu interface reset routine off here
  631. * rather than parcelling it out into one little function
  632. * per register
  633. */
  634. .resetfn = arm_gicv3_icc_reset,
  635. },
  636. };
  637. /**
  638. * vm_change_state_handler - VM change state callback aiming at flushing
  639. * RDIST pending tables into guest RAM
  640. *
  641. * The tables get flushed to guest RAM whenever the VM gets stopped.
  642. */
  643. static void vm_change_state_handler(void *opaque, bool running,
  644. RunState state)
  645. {
  646. GICv3State *s = (GICv3State *)opaque;
  647. Error *err = NULL;
  648. int ret;
  649. if (running) {
  650. return;
  651. }
  652. ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  653. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
  654. NULL, true, &err);
  655. if (err) {
  656. error_report_err(err);
  657. }
  658. if (ret < 0 && ret != -EFAULT) {
  659. abort();
  660. }
  661. }
  662. static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
  663. {
  664. GICv3State *s = KVM_ARM_GICV3(dev);
  665. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  666. bool multiple_redist_region_allowed;
  667. Error *local_err = NULL;
  668. int i;
  669. DPRINTF("kvm_arm_gicv3_realize\n");
  670. kgc->parent_realize(dev, &local_err);
  671. if (local_err) {
  672. error_propagate(errp, local_err);
  673. return;
  674. }
  675. if (s->revision != 3) {
  676. error_setg(errp, "unsupported GIC revision %d for in-kernel GIC",
  677. s->revision);
  678. }
  679. if (s->security_extn) {
  680. error_setg(errp, "the in-kernel VGICv3 does not implement the "
  681. "security extensions");
  682. return;
  683. }
  684. if (s->nmi_support) {
  685. error_setg(errp, "NMI is not supported with the in-kernel GIC");
  686. return;
  687. }
  688. gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL);
  689. for (i = 0; i < s->num_cpu; i++) {
  690. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
  691. define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
  692. }
  693. /* Try to create the device via the device control API */
  694. s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
  695. if (s->dev_fd < 0) {
  696. error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
  697. return;
  698. }
  699. multiple_redist_region_allowed =
  700. kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  701. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION);
  702. if (!multiple_redist_region_allowed && s->nb_redist_regions > 1) {
  703. error_setg(errp, "Multiple VGICv3 redistributor regions are not "
  704. "supported by this host kernel");
  705. error_append_hint(errp, "A maximum of %d VCPUs can be used",
  706. s->redist_region_count[0]);
  707. return;
  708. }
  709. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
  710. 0, &s->num_irq, true, &error_abort);
  711. /* Tell the kernel to complete VGIC initialization now */
  712. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  713. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
  714. kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
  715. KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd, 0);
  716. if (!multiple_redist_region_allowed) {
  717. kvm_arm_register_device(&s->redist_regions[0].iomem, -1,
  718. KVM_DEV_ARM_VGIC_GRP_ADDR,
  719. KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd, 0);
  720. } else {
  721. /* we register regions in reverse order as "devices" are inserted at
  722. * the head of a QSLIST and the list is then popped from the head
  723. * onwards by kvm_arm_machine_init_done()
  724. */
  725. for (i = s->nb_redist_regions - 1; i >= 0; i--) {
  726. /* Address mask made of the rdist region index and count */
  727. uint64_t addr_ormask =
  728. i | ((uint64_t)s->redist_region_count[i] << 52);
  729. kvm_arm_register_device(&s->redist_regions[i].iomem, -1,
  730. KVM_DEV_ARM_VGIC_GRP_ADDR,
  731. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION,
  732. s->dev_fd, addr_ormask);
  733. }
  734. }
  735. if (kvm_has_gsi_routing()) {
  736. /* set up irq routing */
  737. for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
  738. kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
  739. }
  740. kvm_gsi_routing_allowed = true;
  741. kvm_irqchip_commit_routes(kvm_state);
  742. }
  743. if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
  744. GICD_CTLR)) {
  745. error_setg(&s->migration_blocker, "This operating system kernel does "
  746. "not support vGICv3 migration");
  747. if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
  748. return;
  749. }
  750. }
  751. if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  752. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
  753. qemu_add_vm_change_state_handler(vm_change_state_handler, s);
  754. }
  755. }
  756. static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
  757. {
  758. DeviceClass *dc = DEVICE_CLASS(klass);
  759. ResettableClass *rc = RESETTABLE_CLASS(klass);
  760. ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
  761. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
  762. agcc->pre_save = kvm_arm_gicv3_get;
  763. agcc->post_load = kvm_arm_gicv3_put;
  764. device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
  765. &kgc->parent_realize);
  766. resettable_class_set_parent_phases(rc, NULL, kvm_arm_gicv3_reset_hold, NULL,
  767. &kgc->parent_phases);
  768. }
  769. static const TypeInfo kvm_arm_gicv3_info = {
  770. .name = TYPE_KVM_ARM_GICV3,
  771. .parent = TYPE_ARM_GICV3_COMMON,
  772. .instance_size = sizeof(GICv3State),
  773. .class_init = kvm_arm_gicv3_class_init,
  774. .class_size = sizeof(KVMARMGICv3Class),
  775. };
  776. static void kvm_arm_gicv3_register_types(void)
  777. {
  778. type_register_static(&kvm_arm_gicv3_info);
  779. }
  780. type_init(kvm_arm_gicv3_register_types)