arm_gicv3_kvm.c 30 KB

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