arm_gicv3_kvm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 "hw/sysbus.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/module.h"
  27. #include "sysemu/kvm.h"
  28. #include "sysemu/runstate.h"
  29. #include "kvm_arm.h"
  30. #include "gicv3_internal.h"
  31. #include "vgic_common.h"
  32. #include "migration/blocker.h"
  33. #include "qom/object.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. void (*parent_reset)(DeviceState *dev);
  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. case 6:
  412. reg64 = c->icc_apr[GICV3_G0][1];
  413. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
  414. default:
  415. reg64 = c->icc_apr[GICV3_G0][0];
  416. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
  417. }
  418. switch (num_pri_bits) {
  419. case 7:
  420. reg64 = c->icc_apr[GICV3_G1NS][3];
  421. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
  422. reg64 = c->icc_apr[GICV3_G1NS][2];
  423. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
  424. case 6:
  425. reg64 = c->icc_apr[GICV3_G1NS][1];
  426. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
  427. default:
  428. reg64 = c->icc_apr[GICV3_G1NS][0];
  429. kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, true);
  430. }
  431. }
  432. }
  433. static void kvm_arm_gicv3_get(GICv3State *s)
  434. {
  435. uint32_t regl, regh, reg;
  436. uint64_t reg64, redist_typer;
  437. int ncpu, i;
  438. kvm_arm_gicv3_check(s);
  439. kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
  440. kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
  441. redist_typer = ((uint64_t)regh << 32) | regl;
  442. kvm_gicd_access(s, GICD_CTLR, &reg, false);
  443. s->gicd_ctlr = reg;
  444. /* Redistributor state (one per CPU) */
  445. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  446. GICv3CPUState *c = &s->cpu[ncpu];
  447. kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, false);
  448. c->gicr_ctlr = reg;
  449. kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, false);
  450. c->gicr_statusr[GICV3_NS] = reg;
  451. kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, false);
  452. c->gicr_waker = reg;
  453. kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, false);
  454. c->gicr_igroupr0 = reg;
  455. kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, false);
  456. c->gicr_ienabler0 = reg;
  457. kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, false);
  458. c->edge_trigger = half_unshuffle32(reg >> 1) << 16;
  459. kvm_gic_line_level_access(s, 0, ncpu, &reg, false);
  460. c->level = reg;
  461. kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, false);
  462. c->gicr_ipendr0 = reg;
  463. kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, false);
  464. c->gicr_iactiver0 = reg;
  465. for (i = 0; i < GIC_INTERNAL; i += 4) {
  466. kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, false);
  467. c->gicr_ipriorityr[i] = extract32(reg, 0, 8);
  468. c->gicr_ipriorityr[i + 1] = extract32(reg, 8, 8);
  469. c->gicr_ipriorityr[i + 2] = extract32(reg, 16, 8);
  470. c->gicr_ipriorityr[i + 3] = extract32(reg, 24, 8);
  471. }
  472. }
  473. if (redist_typer & GICR_TYPER_PLPIS) {
  474. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  475. GICv3CPUState *c = &s->cpu[ncpu];
  476. kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, false);
  477. kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, false);
  478. c->gicr_propbaser = ((uint64_t)regh << 32) | regl;
  479. kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, false);
  480. kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, false);
  481. c->gicr_pendbaser = ((uint64_t)regh << 32) | regl;
  482. }
  483. }
  484. /* Distributor state (shared between all CPUs */
  485. kvm_gicd_access(s, GICD_STATUSR, &reg, false);
  486. s->gicd_statusr[GICV3_NS] = reg;
  487. /* GICD_IGROUPRn -> s->group bitmap */
  488. kvm_dist_getbmp(s, GICD_IGROUPR, s->group);
  489. /* GICD_ISENABLERn -> s->enabled bitmap */
  490. kvm_dist_getbmp(s, GICD_ISENABLER, s->enabled);
  491. /* Line level of irq */
  492. kvm_gic_get_line_level_bmp(s, s->level);
  493. /* GICD_ISPENDRn -> s->pending bitmap */
  494. kvm_dist_getbmp(s, GICD_ISPENDR, s->pending);
  495. /* GICD_ISACTIVERn -> s->active bitmap */
  496. kvm_dist_getbmp(s, GICD_ISACTIVER, s->active);
  497. /* GICD_ICFGRn -> s->trigger bitmap */
  498. kvm_dist_get_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
  499. /* GICD_IPRIORITYRn -> s->gicd_ipriority[] */
  500. kvm_dist_get_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
  501. /* GICD_IROUTERn -> s->gicd_irouter[irq] */
  502. for (i = GIC_INTERNAL; i < s->num_irq; i++) {
  503. uint32_t offset;
  504. offset = GICD_IROUTER + (sizeof(uint32_t) * i);
  505. kvm_gicd_access(s, offset, &regl, false);
  506. offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
  507. kvm_gicd_access(s, offset, &regh, false);
  508. s->gicd_irouter[i] = ((uint64_t)regh << 32) | regl;
  509. }
  510. /*****************************************************************
  511. * CPU Interface(s) State
  512. */
  513. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  514. GICv3CPUState *c = &s->cpu[ncpu];
  515. int num_pri_bits;
  516. kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, false);
  517. kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
  518. &c->icc_ctlr_el1[GICV3_NS], false);
  519. kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
  520. &c->icc_igrpen[GICV3_G0], false);
  521. kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
  522. &c->icc_igrpen[GICV3_G1NS], false);
  523. kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, false);
  524. kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], false);
  525. kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], false);
  526. num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
  527. ICC_CTLR_EL1_PRIBITS_MASK) >>
  528. ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
  529. switch (num_pri_bits) {
  530. case 7:
  531. kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, false);
  532. c->icc_apr[GICV3_G0][3] = reg64;
  533. kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, false);
  534. c->icc_apr[GICV3_G0][2] = reg64;
  535. case 6:
  536. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
  537. c->icc_apr[GICV3_G0][1] = reg64;
  538. default:
  539. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
  540. c->icc_apr[GICV3_G0][0] = reg64;
  541. }
  542. switch (num_pri_bits) {
  543. case 7:
  544. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
  545. c->icc_apr[GICV3_G1NS][3] = reg64;
  546. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
  547. c->icc_apr[GICV3_G1NS][2] = reg64;
  548. case 6:
  549. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
  550. c->icc_apr[GICV3_G1NS][1] = reg64;
  551. default:
  552. kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
  553. c->icc_apr[GICV3_G1NS][0] = reg64;
  554. }
  555. }
  556. }
  557. static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  558. {
  559. GICv3State *s;
  560. GICv3CPUState *c;
  561. c = (GICv3CPUState *)env->gicv3state;
  562. s = c->gic;
  563. c->icc_pmr_el1 = 0;
  564. c->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
  565. c->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
  566. c->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
  567. c->icc_sre_el1 = 0x7;
  568. memset(c->icc_apr, 0, sizeof(c->icc_apr));
  569. memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
  570. if (s->migration_blocker) {
  571. return;
  572. }
  573. /* Initialize to actual HW supported configuration */
  574. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
  575. KVM_VGIC_ATTR(ICC_CTLR_EL1, c->gicr_typer),
  576. &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
  577. c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
  578. }
  579. static void kvm_arm_gicv3_reset(DeviceState *dev)
  580. {
  581. GICv3State *s = ARM_GICV3_COMMON(dev);
  582. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  583. DPRINTF("Reset\n");
  584. kgc->parent_reset(dev);
  585. if (s->migration_blocker) {
  586. DPRINTF("Cannot put kernel gic state, no kernel interface\n");
  587. return;
  588. }
  589. kvm_arm_gicv3_put(s);
  590. }
  591. /*
  592. * CPU interface registers of GIC needs to be reset on CPU reset.
  593. * For the calling arm_gicv3_icc_reset() on CPU reset, we register
  594. * below ARMCPRegInfo. As we reset the whole cpu interface under single
  595. * register reset, we define only one register of CPU interface instead
  596. * of defining all the registers.
  597. */
  598. static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
  599. { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
  600. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
  601. /*
  602. * If ARM_CP_NOP is used, resetfn is not called,
  603. * So ARM_CP_NO_RAW is appropriate type.
  604. */
  605. .type = ARM_CP_NO_RAW,
  606. .access = PL1_RW,
  607. .readfn = arm_cp_read_zero,
  608. .writefn = arm_cp_write_ignore,
  609. /*
  610. * We hang the whole cpu interface reset routine off here
  611. * rather than parcelling it out into one little function
  612. * per register
  613. */
  614. .resetfn = arm_gicv3_icc_reset,
  615. },
  616. REGINFO_SENTINEL
  617. };
  618. /**
  619. * vm_change_state_handler - VM change state callback aiming at flushing
  620. * RDIST pending tables into guest RAM
  621. *
  622. * The tables get flushed to guest RAM whenever the VM gets stopped.
  623. */
  624. static void vm_change_state_handler(void *opaque, int running,
  625. RunState state)
  626. {
  627. GICv3State *s = (GICv3State *)opaque;
  628. Error *err = NULL;
  629. int ret;
  630. if (running) {
  631. return;
  632. }
  633. ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  634. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
  635. NULL, true, &err);
  636. if (err) {
  637. error_report_err(err);
  638. }
  639. if (ret < 0 && ret != -EFAULT) {
  640. abort();
  641. }
  642. }
  643. static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
  644. {
  645. GICv3State *s = KVM_ARM_GICV3(dev);
  646. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  647. bool multiple_redist_region_allowed;
  648. Error *local_err = NULL;
  649. int i;
  650. DPRINTF("kvm_arm_gicv3_realize\n");
  651. kgc->parent_realize(dev, &local_err);
  652. if (local_err) {
  653. error_propagate(errp, local_err);
  654. return;
  655. }
  656. if (s->security_extn) {
  657. error_setg(errp, "the in-kernel VGICv3 does not implement the "
  658. "security extensions");
  659. return;
  660. }
  661. gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL, &local_err);
  662. if (local_err) {
  663. error_propagate(errp, local_err);
  664. return;
  665. }
  666. for (i = 0; i < s->num_cpu; i++) {
  667. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
  668. define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
  669. }
  670. /* Try to create the device via the device control API */
  671. s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
  672. if (s->dev_fd < 0) {
  673. error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
  674. return;
  675. }
  676. multiple_redist_region_allowed =
  677. kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  678. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION);
  679. if (!multiple_redist_region_allowed && s->nb_redist_regions > 1) {
  680. error_setg(errp, "Multiple VGICv3 redistributor regions are not "
  681. "supported by this host kernel");
  682. error_append_hint(errp, "A maximum of %d VCPUs can be used",
  683. s->redist_region_count[0]);
  684. return;
  685. }
  686. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
  687. 0, &s->num_irq, true, &error_abort);
  688. /* Tell the kernel to complete VGIC initialization now */
  689. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  690. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
  691. kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
  692. KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd, 0);
  693. if (!multiple_redist_region_allowed) {
  694. kvm_arm_register_device(&s->iomem_redist[0], -1,
  695. KVM_DEV_ARM_VGIC_GRP_ADDR,
  696. KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd, 0);
  697. } else {
  698. /* we register regions in reverse order as "devices" are inserted at
  699. * the head of a QSLIST and the list is then popped from the head
  700. * onwards by kvm_arm_machine_init_done()
  701. */
  702. for (i = s->nb_redist_regions - 1; i >= 0; i--) {
  703. /* Address mask made of the rdist region index and count */
  704. uint64_t addr_ormask =
  705. i | ((uint64_t)s->redist_region_count[i] << 52);
  706. kvm_arm_register_device(&s->iomem_redist[i], -1,
  707. KVM_DEV_ARM_VGIC_GRP_ADDR,
  708. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION,
  709. s->dev_fd, addr_ormask);
  710. }
  711. }
  712. if (kvm_has_gsi_routing()) {
  713. /* set up irq routing */
  714. for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
  715. kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
  716. }
  717. kvm_gsi_routing_allowed = true;
  718. kvm_irqchip_commit_routes(kvm_state);
  719. }
  720. if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
  721. GICD_CTLR)) {
  722. error_setg(&s->migration_blocker, "This operating system kernel does "
  723. "not support vGICv3 migration");
  724. if (migrate_add_blocker(s->migration_blocker, errp) < 0) {
  725. error_free(s->migration_blocker);
  726. return;
  727. }
  728. }
  729. if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  730. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
  731. qemu_add_vm_change_state_handler(vm_change_state_handler, s);
  732. }
  733. }
  734. static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
  735. {
  736. DeviceClass *dc = DEVICE_CLASS(klass);
  737. ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
  738. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
  739. agcc->pre_save = kvm_arm_gicv3_get;
  740. agcc->post_load = kvm_arm_gicv3_put;
  741. device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
  742. &kgc->parent_realize);
  743. device_class_set_parent_reset(dc, kvm_arm_gicv3_reset, &kgc->parent_reset);
  744. }
  745. static const TypeInfo kvm_arm_gicv3_info = {
  746. .name = TYPE_KVM_ARM_GICV3,
  747. .parent = TYPE_ARM_GICV3_COMMON,
  748. .instance_size = sizeof(GICv3State),
  749. .class_init = kvm_arm_gicv3_class_init,
  750. .class_size = sizeof(KVMARMGICv3Class),
  751. };
  752. static void kvm_arm_gicv3_register_types(void)
  753. {
  754. type_register_static(&kvm_arm_gicv3_info);
  755. }
  756. type_init(kvm_arm_gicv3_register_types)