arm_gicv3_kvm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. #ifdef DEBUG_GICV3_KVM
  34. #define DPRINTF(fmt, ...) \
  35. do { fprintf(stderr, "kvm_gicv3: " fmt, ## __VA_ARGS__); } while (0)
  36. #else
  37. #define DPRINTF(fmt, ...) \
  38. do { } while (0)
  39. #endif
  40. #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
  41. #define KVM_ARM_GICV3(obj) \
  42. OBJECT_CHECK(GICv3State, (obj), TYPE_KVM_ARM_GICV3)
  43. #define KVM_ARM_GICV3_CLASS(klass) \
  44. OBJECT_CLASS_CHECK(KVMARMGICv3Class, (klass), TYPE_KVM_ARM_GICV3)
  45. #define KVM_ARM_GICV3_GET_CLASS(obj) \
  46. OBJECT_GET_CLASS(KVMARMGICv3Class, (obj), TYPE_KVM_ARM_GICV3)
  47. #define KVM_DEV_ARM_VGIC_SYSREG(op0, op1, crn, crm, op2) \
  48. (ARM64_SYS_REG_SHIFT_MASK(op0, OP0) | \
  49. ARM64_SYS_REG_SHIFT_MASK(op1, OP1) | \
  50. ARM64_SYS_REG_SHIFT_MASK(crn, CRN) | \
  51. ARM64_SYS_REG_SHIFT_MASK(crm, CRM) | \
  52. ARM64_SYS_REG_SHIFT_MASK(op2, OP2))
  53. #define ICC_PMR_EL1 \
  54. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 4, 6, 0)
  55. #define ICC_BPR0_EL1 \
  56. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 3)
  57. #define ICC_AP0R_EL1(n) \
  58. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 8, 4 | n)
  59. #define ICC_AP1R_EL1(n) \
  60. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 9, n)
  61. #define ICC_BPR1_EL1 \
  62. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 3)
  63. #define ICC_CTLR_EL1 \
  64. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 4)
  65. #define ICC_SRE_EL1 \
  66. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 5)
  67. #define ICC_IGRPEN0_EL1 \
  68. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 6)
  69. #define ICC_IGRPEN1_EL1 \
  70. KVM_DEV_ARM_VGIC_SYSREG(3, 0, 12, 12, 7)
  71. typedef struct KVMARMGICv3Class {
  72. ARMGICv3CommonClass parent_class;
  73. DeviceRealize parent_realize;
  74. void (*parent_reset)(DeviceState *dev);
  75. } KVMARMGICv3Class;
  76. static void kvm_arm_gicv3_set_irq(void *opaque, int irq, int level)
  77. {
  78. GICv3State *s = (GICv3State *)opaque;
  79. kvm_arm_gic_set_irq(s->num_irq, irq, level);
  80. }
  81. #define KVM_VGIC_ATTR(reg, typer) \
  82. ((typer & KVM_DEV_ARM_VGIC_V3_MPIDR_MASK) | (reg))
  83. static inline void kvm_gicd_access(GICv3State *s, int offset,
  84. uint32_t *val, bool write)
  85. {
  86. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
  87. KVM_VGIC_ATTR(offset, 0),
  88. val, write, &error_abort);
  89. }
  90. static inline void kvm_gicr_access(GICv3State *s, int offset, int cpu,
  91. uint32_t *val, bool write)
  92. {
  93. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
  94. KVM_VGIC_ATTR(offset, s->cpu[cpu].gicr_typer),
  95. val, write, &error_abort);
  96. }
  97. static inline void kvm_gicc_access(GICv3State *s, uint64_t reg, int cpu,
  98. uint64_t *val, bool write)
  99. {
  100. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
  101. KVM_VGIC_ATTR(reg, s->cpu[cpu].gicr_typer),
  102. val, write, &error_abort);
  103. }
  104. static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu,
  105. uint32_t *val, bool write)
  106. {
  107. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO,
  108. KVM_VGIC_ATTR(irq, s->cpu[cpu].gicr_typer) |
  109. (VGIC_LEVEL_INFO_LINE_LEVEL <<
  110. KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT),
  111. val, write, &error_abort);
  112. }
  113. /* Loop through each distributor IRQ related register; since bits
  114. * corresponding to SPIs and PPIs are RAZ/WI when affinity routing
  115. * is enabled, we skip those.
  116. */
  117. #define for_each_dist_irq_reg(_irq, _max, _field_width) \
  118. for (_irq = GIC_INTERNAL; _irq < _max; _irq += (32 / _field_width))
  119. static void kvm_dist_get_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
  120. {
  121. uint32_t reg, *field;
  122. int irq;
  123. /* For the KVM GICv3, affinity routing is always enabled, and the first 8
  124. * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
  125. * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
  126. * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
  127. * offset.
  128. */
  129. field = (uint32_t *)(bmp + GIC_INTERNAL);
  130. offset += (GIC_INTERNAL * 8) / 8;
  131. for_each_dist_irq_reg(irq, s->num_irq, 8) {
  132. kvm_gicd_access(s, offset, &reg, false);
  133. *field = reg;
  134. offset += 4;
  135. field++;
  136. }
  137. }
  138. static void kvm_dist_put_priority(GICv3State *s, uint32_t offset, uint8_t *bmp)
  139. {
  140. uint32_t reg, *field;
  141. int irq;
  142. /* For the KVM GICv3, affinity routing is always enabled, and the first 8
  143. * GICD_IPRIORITYR<n> registers are always RAZ/WI. The corresponding
  144. * functionality is replaced by GICR_IPRIORITYR<n>. It doesn't need to
  145. * sync them. So it needs to skip the field of GIC_INTERNAL irqs in bmp and
  146. * offset.
  147. */
  148. field = (uint32_t *)(bmp + GIC_INTERNAL);
  149. offset += (GIC_INTERNAL * 8) / 8;
  150. for_each_dist_irq_reg(irq, s->num_irq, 8) {
  151. reg = *field;
  152. kvm_gicd_access(s, offset, &reg, true);
  153. offset += 4;
  154. field++;
  155. }
  156. }
  157. static void kvm_dist_get_edge_trigger(GICv3State *s, uint32_t offset,
  158. uint32_t *bmp)
  159. {
  160. uint32_t reg;
  161. int irq;
  162. /* For the KVM GICv3, affinity routing is always enabled, and the first 2
  163. * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
  164. * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
  165. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  166. * This matches the for_each_dist_irq_reg() macro which also skips the
  167. * first GIC_INTERNAL irqs.
  168. */
  169. offset += (GIC_INTERNAL * 2) / 8;
  170. for_each_dist_irq_reg(irq, s->num_irq, 2) {
  171. kvm_gicd_access(s, offset, &reg, false);
  172. reg = half_unshuffle32(reg >> 1);
  173. if (irq % 32 != 0) {
  174. reg = (reg << 16);
  175. }
  176. *gic_bmp_ptr32(bmp, irq) |= reg;
  177. offset += 4;
  178. }
  179. }
  180. static void kvm_dist_put_edge_trigger(GICv3State *s, uint32_t offset,
  181. uint32_t *bmp)
  182. {
  183. uint32_t reg;
  184. int irq;
  185. /* For the KVM GICv3, affinity routing is always enabled, and the first 2
  186. * GICD_ICFGR<n> registers are always RAZ/WI. The corresponding
  187. * functionality is replaced by GICR_ICFGR<n>. It doesn't need to sync
  188. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  189. * This matches the for_each_dist_irq_reg() macro which also skips the
  190. * first GIC_INTERNAL irqs.
  191. */
  192. offset += (GIC_INTERNAL * 2) / 8;
  193. for_each_dist_irq_reg(irq, s->num_irq, 2) {
  194. reg = *gic_bmp_ptr32(bmp, irq);
  195. if (irq % 32 != 0) {
  196. reg = (reg & 0xffff0000) >> 16;
  197. } else {
  198. reg = reg & 0xffff;
  199. }
  200. reg = half_shuffle32(reg) << 1;
  201. kvm_gicd_access(s, offset, &reg, true);
  202. offset += 4;
  203. }
  204. }
  205. static void kvm_gic_get_line_level_bmp(GICv3State *s, uint32_t *bmp)
  206. {
  207. uint32_t reg;
  208. int irq;
  209. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  210. kvm_gic_line_level_access(s, irq, 0, &reg, false);
  211. *gic_bmp_ptr32(bmp, irq) = reg;
  212. }
  213. }
  214. static void kvm_gic_put_line_level_bmp(GICv3State *s, uint32_t *bmp)
  215. {
  216. uint32_t reg;
  217. int irq;
  218. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  219. reg = *gic_bmp_ptr32(bmp, irq);
  220. kvm_gic_line_level_access(s, irq, 0, &reg, true);
  221. }
  222. }
  223. /* Read a bitmap register group from the kernel VGIC. */
  224. static void kvm_dist_getbmp(GICv3State *s, uint32_t offset, uint32_t *bmp)
  225. {
  226. uint32_t reg;
  227. int irq;
  228. /* For the KVM GICv3, affinity routing is always enabled, and the
  229. * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
  230. * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
  231. * functionality is replaced by the GICR registers. It doesn't need to sync
  232. * them. So it should increase the offset to skip GIC_INTERNAL irqs.
  233. * This matches the for_each_dist_irq_reg() macro which also skips the
  234. * first GIC_INTERNAL irqs.
  235. */
  236. offset += (GIC_INTERNAL * 1) / 8;
  237. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  238. kvm_gicd_access(s, offset, &reg, false);
  239. *gic_bmp_ptr32(bmp, irq) = reg;
  240. offset += 4;
  241. }
  242. }
  243. static void kvm_dist_putbmp(GICv3State *s, uint32_t offset,
  244. uint32_t clroffset, uint32_t *bmp)
  245. {
  246. uint32_t reg;
  247. int irq;
  248. /* For the KVM GICv3, affinity routing is always enabled, and the
  249. * GICD_IGROUPR0/GICD_IGRPMODR0/GICD_ISENABLER0/GICD_ISPENDR0/
  250. * GICD_ISACTIVER0 registers are always RAZ/WI. The corresponding
  251. * functionality is replaced by the GICR registers. It doesn't need to sync
  252. * them. So it should increase the offset and clroffset to skip GIC_INTERNAL
  253. * irqs. This matches the for_each_dist_irq_reg() macro which also skips the
  254. * first GIC_INTERNAL irqs.
  255. */
  256. offset += (GIC_INTERNAL * 1) / 8;
  257. if (clroffset != 0) {
  258. clroffset += (GIC_INTERNAL * 1) / 8;
  259. }
  260. for_each_dist_irq_reg(irq, s->num_irq, 1) {
  261. /* If this bitmap is a set/clear register pair, first write to the
  262. * clear-reg to clear all bits before using the set-reg to write
  263. * the 1 bits.
  264. */
  265. if (clroffset != 0) {
  266. reg = 0;
  267. kvm_gicd_access(s, clroffset, &reg, true);
  268. clroffset += 4;
  269. }
  270. reg = *gic_bmp_ptr32(bmp, irq);
  271. kvm_gicd_access(s, offset, &reg, true);
  272. offset += 4;
  273. }
  274. }
  275. static void kvm_arm_gicv3_check(GICv3State *s)
  276. {
  277. uint32_t reg;
  278. uint32_t num_irq;
  279. /* Sanity checking s->num_irq */
  280. kvm_gicd_access(s, GICD_TYPER, &reg, false);
  281. num_irq = ((reg & 0x1f) + 1) * 32;
  282. if (num_irq < s->num_irq) {
  283. error_report("Model requests %u IRQs, but kernel supports max %u",
  284. s->num_irq, num_irq);
  285. abort();
  286. }
  287. }
  288. static void kvm_arm_gicv3_put(GICv3State *s)
  289. {
  290. uint32_t regl, regh, reg;
  291. uint64_t reg64, redist_typer;
  292. int ncpu, i;
  293. kvm_arm_gicv3_check(s);
  294. kvm_gicr_access(s, GICR_TYPER, 0, &regl, false);
  295. kvm_gicr_access(s, GICR_TYPER + 4, 0, &regh, false);
  296. redist_typer = ((uint64_t)regh << 32) | regl;
  297. reg = s->gicd_ctlr;
  298. kvm_gicd_access(s, GICD_CTLR, &reg, true);
  299. if (redist_typer & GICR_TYPER_PLPIS) {
  300. /* Set base addresses before LPIs are enabled by GICR_CTLR write */
  301. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  302. GICv3CPUState *c = &s->cpu[ncpu];
  303. reg64 = c->gicr_propbaser;
  304. regl = (uint32_t)reg64;
  305. kvm_gicr_access(s, GICR_PROPBASER, ncpu, &regl, true);
  306. regh = (uint32_t)(reg64 >> 32);
  307. kvm_gicr_access(s, GICR_PROPBASER + 4, ncpu, &regh, true);
  308. reg64 = c->gicr_pendbaser;
  309. if (!(c->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
  310. /* Setting PTZ is advised if LPIs are disabled, to reduce
  311. * GIC initialization time.
  312. */
  313. reg64 |= GICR_PENDBASER_PTZ;
  314. }
  315. regl = (uint32_t)reg64;
  316. kvm_gicr_access(s, GICR_PENDBASER, ncpu, &regl, true);
  317. regh = (uint32_t)(reg64 >> 32);
  318. kvm_gicr_access(s, GICR_PENDBASER + 4, ncpu, &regh, true);
  319. }
  320. }
  321. /* Redistributor state (one per CPU) */
  322. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  323. GICv3CPUState *c = &s->cpu[ncpu];
  324. reg = c->gicr_ctlr;
  325. kvm_gicr_access(s, GICR_CTLR, ncpu, &reg, true);
  326. reg = c->gicr_statusr[GICV3_NS];
  327. kvm_gicr_access(s, GICR_STATUSR, ncpu, &reg, true);
  328. reg = c->gicr_waker;
  329. kvm_gicr_access(s, GICR_WAKER, ncpu, &reg, true);
  330. reg = c->gicr_igroupr0;
  331. kvm_gicr_access(s, GICR_IGROUPR0, ncpu, &reg, true);
  332. reg = ~0;
  333. kvm_gicr_access(s, GICR_ICENABLER0, ncpu, &reg, true);
  334. reg = c->gicr_ienabler0;
  335. kvm_gicr_access(s, GICR_ISENABLER0, ncpu, &reg, true);
  336. /* Restore config before pending so we treat level/edge correctly */
  337. reg = half_shuffle32(c->edge_trigger >> 16) << 1;
  338. kvm_gicr_access(s, GICR_ICFGR1, ncpu, &reg, true);
  339. reg = c->level;
  340. kvm_gic_line_level_access(s, 0, ncpu, &reg, true);
  341. reg = ~0;
  342. kvm_gicr_access(s, GICR_ICPENDR0, ncpu, &reg, true);
  343. reg = c->gicr_ipendr0;
  344. kvm_gicr_access(s, GICR_ISPENDR0, ncpu, &reg, true);
  345. reg = ~0;
  346. kvm_gicr_access(s, GICR_ICACTIVER0, ncpu, &reg, true);
  347. reg = c->gicr_iactiver0;
  348. kvm_gicr_access(s, GICR_ISACTIVER0, ncpu, &reg, true);
  349. for (i = 0; i < GIC_INTERNAL; i += 4) {
  350. reg = c->gicr_ipriorityr[i] |
  351. (c->gicr_ipriorityr[i + 1] << 8) |
  352. (c->gicr_ipriorityr[i + 2] << 16) |
  353. (c->gicr_ipriorityr[i + 3] << 24);
  354. kvm_gicr_access(s, GICR_IPRIORITYR + i, ncpu, &reg, true);
  355. }
  356. }
  357. /* Distributor state (shared between all CPUs */
  358. reg = s->gicd_statusr[GICV3_NS];
  359. kvm_gicd_access(s, GICD_STATUSR, &reg, true);
  360. /* s->enable bitmap -> GICD_ISENABLERn */
  361. kvm_dist_putbmp(s, GICD_ISENABLER, GICD_ICENABLER, s->enabled);
  362. /* s->group bitmap -> GICD_IGROUPRn */
  363. kvm_dist_putbmp(s, GICD_IGROUPR, 0, s->group);
  364. /* Restore targets before pending to ensure the pending state is set on
  365. * the appropriate CPU interfaces in the kernel
  366. */
  367. /* s->gicd_irouter[irq] -> GICD_IROUTERn
  368. * We can't use kvm_dist_put() here because the registers are 64-bit
  369. */
  370. for (i = GIC_INTERNAL; i < s->num_irq; i++) {
  371. uint32_t offset;
  372. offset = GICD_IROUTER + (sizeof(uint32_t) * i);
  373. reg = (uint32_t)s->gicd_irouter[i];
  374. kvm_gicd_access(s, offset, &reg, true);
  375. offset = GICD_IROUTER + (sizeof(uint32_t) * i) + 4;
  376. reg = (uint32_t)(s->gicd_irouter[i] >> 32);
  377. kvm_gicd_access(s, offset, &reg, true);
  378. }
  379. /* s->trigger bitmap -> GICD_ICFGRn
  380. * (restore configuration registers before pending IRQs so we treat
  381. * level/edge correctly)
  382. */
  383. kvm_dist_put_edge_trigger(s, GICD_ICFGR, s->edge_trigger);
  384. /* s->level bitmap -> line_level */
  385. kvm_gic_put_line_level_bmp(s, s->level);
  386. /* s->pending bitmap -> GICD_ISPENDRn */
  387. kvm_dist_putbmp(s, GICD_ISPENDR, GICD_ICPENDR, s->pending);
  388. /* s->active bitmap -> GICD_ISACTIVERn */
  389. kvm_dist_putbmp(s, GICD_ISACTIVER, GICD_ICACTIVER, s->active);
  390. /* s->gicd_ipriority[] -> GICD_IPRIORITYRn */
  391. kvm_dist_put_priority(s, GICD_IPRIORITYR, s->gicd_ipriority);
  392. /* CPU Interface state (one per CPU) */
  393. for (ncpu = 0; ncpu < s->num_cpu; ncpu++) {
  394. GICv3CPUState *c = &s->cpu[ncpu];
  395. int num_pri_bits;
  396. kvm_gicc_access(s, ICC_SRE_EL1, ncpu, &c->icc_sre_el1, true);
  397. kvm_gicc_access(s, ICC_CTLR_EL1, ncpu,
  398. &c->icc_ctlr_el1[GICV3_NS], true);
  399. kvm_gicc_access(s, ICC_IGRPEN0_EL1, ncpu,
  400. &c->icc_igrpen[GICV3_G0], true);
  401. kvm_gicc_access(s, ICC_IGRPEN1_EL1, ncpu,
  402. &c->icc_igrpen[GICV3_G1NS], true);
  403. kvm_gicc_access(s, ICC_PMR_EL1, ncpu, &c->icc_pmr_el1, true);
  404. kvm_gicc_access(s, ICC_BPR0_EL1, ncpu, &c->icc_bpr[GICV3_G0], true);
  405. kvm_gicc_access(s, ICC_BPR1_EL1, ncpu, &c->icc_bpr[GICV3_G1NS], true);
  406. num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS] &
  407. ICC_CTLR_EL1_PRIBITS_MASK) >>
  408. ICC_CTLR_EL1_PRIBITS_SHIFT) + 1;
  409. switch (num_pri_bits) {
  410. case 7:
  411. reg64 = c->icc_apr[GICV3_G0][3];
  412. kvm_gicc_access(s, ICC_AP0R_EL1(3), ncpu, &reg64, true);
  413. reg64 = c->icc_apr[GICV3_G0][2];
  414. kvm_gicc_access(s, ICC_AP0R_EL1(2), ncpu, &reg64, true);
  415. case 6:
  416. reg64 = c->icc_apr[GICV3_G0][1];
  417. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, true);
  418. default:
  419. reg64 = c->icc_apr[GICV3_G0][0];
  420. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, true);
  421. }
  422. switch (num_pri_bits) {
  423. case 7:
  424. reg64 = c->icc_apr[GICV3_G1NS][3];
  425. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, true);
  426. reg64 = c->icc_apr[GICV3_G1NS][2];
  427. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, true);
  428. case 6:
  429. reg64 = c->icc_apr[GICV3_G1NS][1];
  430. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, true);
  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. case 6:
  540. kvm_gicc_access(s, ICC_AP0R_EL1(1), ncpu, &reg64, false);
  541. c->icc_apr[GICV3_G0][1] = reg64;
  542. default:
  543. kvm_gicc_access(s, ICC_AP0R_EL1(0), ncpu, &reg64, false);
  544. c->icc_apr[GICV3_G0][0] = reg64;
  545. }
  546. switch (num_pri_bits) {
  547. case 7:
  548. kvm_gicc_access(s, ICC_AP1R_EL1(3), ncpu, &reg64, false);
  549. c->icc_apr[GICV3_G1NS][3] = reg64;
  550. kvm_gicc_access(s, ICC_AP1R_EL1(2), ncpu, &reg64, false);
  551. c->icc_apr[GICV3_G1NS][2] = reg64;
  552. case 6:
  553. kvm_gicc_access(s, ICC_AP1R_EL1(1), ncpu, &reg64, false);
  554. c->icc_apr[GICV3_G1NS][1] = reg64;
  555. default:
  556. kvm_gicc_access(s, ICC_AP1R_EL1(0), ncpu, &reg64, false);
  557. c->icc_apr[GICV3_G1NS][0] = reg64;
  558. }
  559. }
  560. }
  561. static void arm_gicv3_icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
  562. {
  563. ARMCPU *cpu;
  564. GICv3State *s;
  565. GICv3CPUState *c;
  566. c = (GICv3CPUState *)env->gicv3state;
  567. s = c->gic;
  568. cpu = ARM_CPU(c->cpu);
  569. c->icc_pmr_el1 = 0;
  570. c->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
  571. c->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
  572. c->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
  573. c->icc_sre_el1 = 0x7;
  574. memset(c->icc_apr, 0, sizeof(c->icc_apr));
  575. memset(c->icc_igrpen, 0, sizeof(c->icc_igrpen));
  576. if (s->migration_blocker) {
  577. return;
  578. }
  579. /* Initialize to actual HW supported configuration */
  580. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS,
  581. KVM_VGIC_ATTR(ICC_CTLR_EL1, cpu->mp_affinity),
  582. &c->icc_ctlr_el1[GICV3_NS], false, &error_abort);
  583. c->icc_ctlr_el1[GICV3_S] = c->icc_ctlr_el1[GICV3_NS];
  584. }
  585. static void kvm_arm_gicv3_reset(DeviceState *dev)
  586. {
  587. GICv3State *s = ARM_GICV3_COMMON(dev);
  588. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  589. DPRINTF("Reset\n");
  590. kgc->parent_reset(dev);
  591. if (s->migration_blocker) {
  592. DPRINTF("Cannot put kernel gic state, no kernel interface\n");
  593. return;
  594. }
  595. kvm_arm_gicv3_put(s);
  596. }
  597. /*
  598. * CPU interface registers of GIC needs to be reset on CPU reset.
  599. * For the calling arm_gicv3_icc_reset() on CPU reset, we register
  600. * below ARMCPRegInfo. As we reset the whole cpu interface under single
  601. * register reset, we define only one register of CPU interface instead
  602. * of defining all the registers.
  603. */
  604. static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
  605. { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
  606. .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
  607. /*
  608. * If ARM_CP_NOP is used, resetfn is not called,
  609. * So ARM_CP_NO_RAW is appropriate type.
  610. */
  611. .type = ARM_CP_NO_RAW,
  612. .access = PL1_RW,
  613. .readfn = arm_cp_read_zero,
  614. .writefn = arm_cp_write_ignore,
  615. /*
  616. * We hang the whole cpu interface reset routine off here
  617. * rather than parcelling it out into one little function
  618. * per register
  619. */
  620. .resetfn = arm_gicv3_icc_reset,
  621. },
  622. REGINFO_SENTINEL
  623. };
  624. /**
  625. * vm_change_state_handler - VM change state callback aiming at flushing
  626. * RDIST pending tables into guest RAM
  627. *
  628. * The tables get flushed to guest RAM whenever the VM gets stopped.
  629. */
  630. static void vm_change_state_handler(void *opaque, int running,
  631. RunState state)
  632. {
  633. GICv3State *s = (GICv3State *)opaque;
  634. Error *err = NULL;
  635. int ret;
  636. if (running) {
  637. return;
  638. }
  639. ret = kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  640. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES,
  641. NULL, true, &err);
  642. if (err) {
  643. error_report_err(err);
  644. }
  645. if (ret < 0 && ret != -EFAULT) {
  646. abort();
  647. }
  648. }
  649. static void kvm_arm_gicv3_realize(DeviceState *dev, Error **errp)
  650. {
  651. GICv3State *s = KVM_ARM_GICV3(dev);
  652. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_GET_CLASS(s);
  653. bool multiple_redist_region_allowed;
  654. Error *local_err = NULL;
  655. int i;
  656. DPRINTF("kvm_arm_gicv3_realize\n");
  657. kgc->parent_realize(dev, &local_err);
  658. if (local_err) {
  659. error_propagate(errp, local_err);
  660. return;
  661. }
  662. if (s->security_extn) {
  663. error_setg(errp, "the in-kernel VGICv3 does not implement the "
  664. "security extensions");
  665. return;
  666. }
  667. gicv3_init_irqs_and_mmio(s, kvm_arm_gicv3_set_irq, NULL, &local_err);
  668. if (local_err) {
  669. error_propagate(errp, local_err);
  670. return;
  671. }
  672. for (i = 0; i < s->num_cpu; i++) {
  673. ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
  674. define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
  675. }
  676. /* Try to create the device via the device control API */
  677. s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V3, false);
  678. if (s->dev_fd < 0) {
  679. error_setg_errno(errp, -s->dev_fd, "error creating in-kernel VGIC");
  680. return;
  681. }
  682. multiple_redist_region_allowed =
  683. kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ADDR,
  684. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION);
  685. if (!multiple_redist_region_allowed && s->nb_redist_regions > 1) {
  686. error_setg(errp, "Multiple VGICv3 redistributor regions are not "
  687. "supported by this host kernel");
  688. error_append_hint(errp, "A maximum of %d VCPUs can be used",
  689. s->redist_region_count[0]);
  690. return;
  691. }
  692. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
  693. 0, &s->num_irq, true, &error_abort);
  694. /* Tell the kernel to complete VGIC initialization now */
  695. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  696. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
  697. kvm_arm_register_device(&s->iomem_dist, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
  698. KVM_VGIC_V3_ADDR_TYPE_DIST, s->dev_fd, 0);
  699. if (!multiple_redist_region_allowed) {
  700. kvm_arm_register_device(&s->iomem_redist[0], -1,
  701. KVM_DEV_ARM_VGIC_GRP_ADDR,
  702. KVM_VGIC_V3_ADDR_TYPE_REDIST, s->dev_fd, 0);
  703. } else {
  704. /* we register regions in reverse order as "devices" are inserted at
  705. * the head of a QSLIST and the list is then popped from the head
  706. * onwards by kvm_arm_machine_init_done()
  707. */
  708. for (i = s->nb_redist_regions - 1; i >= 0; i--) {
  709. /* Address mask made of the rdist region index and count */
  710. uint64_t addr_ormask =
  711. i | ((uint64_t)s->redist_region_count[i] << 52);
  712. kvm_arm_register_device(&s->iomem_redist[i], -1,
  713. KVM_DEV_ARM_VGIC_GRP_ADDR,
  714. KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION,
  715. s->dev_fd, addr_ormask);
  716. }
  717. }
  718. if (kvm_has_gsi_routing()) {
  719. /* set up irq routing */
  720. for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
  721. kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
  722. }
  723. kvm_gsi_routing_allowed = true;
  724. kvm_irqchip_commit_routes(kvm_state);
  725. }
  726. if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
  727. GICD_CTLR)) {
  728. error_setg(&s->migration_blocker, "This operating system kernel does "
  729. "not support vGICv3 migration");
  730. migrate_add_blocker(s->migration_blocker, &local_err);
  731. if (local_err) {
  732. error_propagate(errp, local_err);
  733. error_free(s->migration_blocker);
  734. return;
  735. }
  736. }
  737. if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  738. KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) {
  739. qemu_add_vm_change_state_handler(vm_change_state_handler, s);
  740. }
  741. }
  742. static void kvm_arm_gicv3_class_init(ObjectClass *klass, void *data)
  743. {
  744. DeviceClass *dc = DEVICE_CLASS(klass);
  745. ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
  746. KVMARMGICv3Class *kgc = KVM_ARM_GICV3_CLASS(klass);
  747. agcc->pre_save = kvm_arm_gicv3_get;
  748. agcc->post_load = kvm_arm_gicv3_put;
  749. device_class_set_parent_realize(dc, kvm_arm_gicv3_realize,
  750. &kgc->parent_realize);
  751. device_class_set_parent_reset(dc, kvm_arm_gicv3_reset, &kgc->parent_reset);
  752. }
  753. static const TypeInfo kvm_arm_gicv3_info = {
  754. .name = TYPE_KVM_ARM_GICV3,
  755. .parent = TYPE_ARM_GICV3_COMMON,
  756. .instance_size = sizeof(GICv3State),
  757. .class_init = kvm_arm_gicv3_class_init,
  758. .class_size = sizeof(KVMARMGICv3Class),
  759. };
  760. static void kvm_arm_gicv3_register_types(void)
  761. {
  762. type_register_static(&kvm_arm_gicv3_info);
  763. }
  764. type_init(kvm_arm_gicv3_register_types)