2
0

arm_gic_kvm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * ARM Generic Interrupt Controller using KVM in-kernel support
  3. *
  4. * Copyright (c) 2012 Linaro Limited
  5. * Written by Peter Maydell
  6. * Save/Restore logic added by Christoffer Dall.
  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 "qemu/module.h"
  24. #include "migration/blocker.h"
  25. #include "system/kvm.h"
  26. #include "kvm_arm.h"
  27. #include "gic_internal.h"
  28. #include "vgic_common.h"
  29. #include "qom/object.h"
  30. #define TYPE_KVM_ARM_GIC "kvm-arm-gic"
  31. typedef struct KVMARMGICClass KVMARMGICClass;
  32. /* This is reusing the GICState typedef from ARM_GIC_COMMON */
  33. DECLARE_OBJ_CHECKERS(GICState, KVMARMGICClass,
  34. KVM_ARM_GIC, TYPE_KVM_ARM_GIC)
  35. struct KVMARMGICClass {
  36. ARMGICCommonClass parent_class;
  37. DeviceRealize parent_realize;
  38. ResettablePhases parent_phases;
  39. };
  40. void kvm_arm_gic_set_irq(uint32_t num_irq, int irq, int level)
  41. {
  42. /* Meaning of the 'irq' parameter:
  43. * [0..N-1] : external interrupts
  44. * [N..N+31] : PPI (internal) interrupts for CPU 0
  45. * [N+32..N+63] : PPI (internal interrupts for CPU 1
  46. * ...
  47. * Convert this to the kernel's desired encoding, which
  48. * has separate fields in the irq number for type,
  49. * CPU number and interrupt number.
  50. */
  51. int irqtype, cpu;
  52. if (irq < (num_irq - GIC_INTERNAL)) {
  53. /* External interrupt. The kernel numbers these like the GIC
  54. * hardware, with external interrupt IDs starting after the
  55. * internal ones.
  56. */
  57. irqtype = KVM_ARM_IRQ_TYPE_SPI;
  58. cpu = 0;
  59. irq += GIC_INTERNAL;
  60. } else {
  61. /* Internal interrupt: decode into (cpu, interrupt id) */
  62. irqtype = KVM_ARM_IRQ_TYPE_PPI;
  63. irq -= (num_irq - GIC_INTERNAL);
  64. cpu = irq / GIC_INTERNAL;
  65. irq %= GIC_INTERNAL;
  66. }
  67. kvm_arm_set_irq(cpu, irqtype, irq, !!level);
  68. }
  69. static void kvm_arm_gicv2_set_irq(void *opaque, int irq, int level)
  70. {
  71. GICState *s = (GICState *)opaque;
  72. kvm_arm_gic_set_irq(s->num_irq, irq, level);
  73. }
  74. static bool kvm_arm_gic_can_save_restore(GICState *s)
  75. {
  76. return s->dev_fd >= 0;
  77. }
  78. #define KVM_VGIC_ATTR(offset, cpu) \
  79. ((((uint64_t)(cpu) << KVM_DEV_ARM_VGIC_CPUID_SHIFT) & \
  80. KVM_DEV_ARM_VGIC_CPUID_MASK) | \
  81. (((uint64_t)(offset) << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) & \
  82. KVM_DEV_ARM_VGIC_OFFSET_MASK))
  83. static void kvm_gicd_access(GICState *s, int offset, int cpu,
  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, cpu), val, write, &error_abort);
  88. }
  89. static void kvm_gicc_access(GICState *s, int offset, int cpu,
  90. uint32_t *val, bool write)
  91. {
  92. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
  93. KVM_VGIC_ATTR(offset, cpu), val, write, &error_abort);
  94. }
  95. #define for_each_irq_reg(_ctr, _max_irq, _field_width) \
  96. for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++)
  97. /*
  98. * Translate from the in-kernel field for an IRQ value to/from the qemu
  99. * representation.
  100. */
  101. typedef void (*vgic_translate_fn)(GICState *s, int irq, int cpu,
  102. uint32_t *field, bool to_kernel);
  103. /* synthetic translate function used for clear/set registers to completely
  104. * clear a setting using a clear-register before setting the remaining bits
  105. * using a set-register */
  106. static void translate_clear(GICState *s, int irq, int cpu,
  107. uint32_t *field, bool to_kernel)
  108. {
  109. if (to_kernel) {
  110. *field = ~0;
  111. } else {
  112. /* does not make sense: qemu model doesn't use set/clear regs */
  113. abort();
  114. }
  115. }
  116. static void translate_group(GICState *s, int irq, int cpu,
  117. uint32_t *field, bool to_kernel)
  118. {
  119. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  120. if (to_kernel) {
  121. *field = GIC_DIST_TEST_GROUP(irq, cm);
  122. } else {
  123. if (*field & 1) {
  124. GIC_DIST_SET_GROUP(irq, cm);
  125. }
  126. }
  127. }
  128. static void translate_enabled(GICState *s, int irq, int cpu,
  129. uint32_t *field, bool to_kernel)
  130. {
  131. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  132. if (to_kernel) {
  133. *field = GIC_DIST_TEST_ENABLED(irq, cm);
  134. } else {
  135. if (*field & 1) {
  136. GIC_DIST_SET_ENABLED(irq, cm);
  137. }
  138. }
  139. }
  140. static void translate_pending(GICState *s, int irq, int cpu,
  141. uint32_t *field, bool to_kernel)
  142. {
  143. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  144. if (to_kernel) {
  145. *field = gic_test_pending(s, irq, cm);
  146. } else {
  147. if (*field & 1) {
  148. GIC_DIST_SET_PENDING(irq, cm);
  149. /* TODO: Capture is level-line is held high in the kernel */
  150. }
  151. }
  152. }
  153. static void translate_active(GICState *s, int irq, int cpu,
  154. uint32_t *field, bool to_kernel)
  155. {
  156. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  157. if (to_kernel) {
  158. *field = GIC_DIST_TEST_ACTIVE(irq, cm);
  159. } else {
  160. if (*field & 1) {
  161. GIC_DIST_SET_ACTIVE(irq, cm);
  162. }
  163. }
  164. }
  165. static void translate_trigger(GICState *s, int irq, int cpu,
  166. uint32_t *field, bool to_kernel)
  167. {
  168. if (to_kernel) {
  169. *field = (GIC_DIST_TEST_EDGE_TRIGGER(irq)) ? 0x2 : 0x0;
  170. } else {
  171. if (*field & 0x2) {
  172. GIC_DIST_SET_EDGE_TRIGGER(irq);
  173. }
  174. }
  175. }
  176. static void translate_priority(GICState *s, int irq, int cpu,
  177. uint32_t *field, bool to_kernel)
  178. {
  179. if (to_kernel) {
  180. *field = GIC_DIST_GET_PRIORITY(irq, cpu) & 0xff;
  181. } else {
  182. gic_dist_set_priority(s, cpu, irq,
  183. *field & 0xff, MEMTXATTRS_UNSPECIFIED);
  184. }
  185. }
  186. static void translate_targets(GICState *s, int irq, int cpu,
  187. uint32_t *field, bool to_kernel)
  188. {
  189. if (to_kernel) {
  190. *field = s->irq_target[irq] & 0xff;
  191. } else {
  192. s->irq_target[irq] = *field & 0xff;
  193. }
  194. }
  195. static void translate_sgisource(GICState *s, int irq, int cpu,
  196. uint32_t *field, bool to_kernel)
  197. {
  198. if (to_kernel) {
  199. *field = s->sgi_pending[irq][cpu] & 0xff;
  200. } else {
  201. s->sgi_pending[irq][cpu] = *field & 0xff;
  202. }
  203. }
  204. /* Read a register group from the kernel VGIC */
  205. static void kvm_dist_get(GICState *s, uint32_t offset, int width,
  206. int maxirq, vgic_translate_fn translate_fn)
  207. {
  208. uint32_t reg;
  209. int i;
  210. int j;
  211. int irq;
  212. int cpu;
  213. int regsz = 32 / width; /* irqs per kernel register */
  214. uint32_t field;
  215. for_each_irq_reg(i, maxirq, width) {
  216. irq = i * regsz;
  217. cpu = 0;
  218. while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
  219. kvm_gicd_access(s, offset, cpu, &reg, false);
  220. for (j = 0; j < regsz; j++) {
  221. field = extract32(reg, j * width, width);
  222. translate_fn(s, irq + j, cpu, &field, false);
  223. }
  224. cpu++;
  225. }
  226. offset += 4;
  227. }
  228. }
  229. /* Write a register group to the kernel VGIC */
  230. static void kvm_dist_put(GICState *s, uint32_t offset, int width,
  231. int maxirq, vgic_translate_fn translate_fn)
  232. {
  233. uint32_t reg;
  234. int i;
  235. int j;
  236. int irq;
  237. int cpu;
  238. int regsz = 32 / width; /* irqs per kernel register */
  239. uint32_t field;
  240. for_each_irq_reg(i, maxirq, width) {
  241. irq = i * regsz;
  242. cpu = 0;
  243. while ((cpu < s->num_cpu && irq < GIC_INTERNAL) || cpu == 0) {
  244. reg = 0;
  245. for (j = 0; j < regsz; j++) {
  246. translate_fn(s, irq + j, cpu, &field, true);
  247. reg = deposit32(reg, j * width, width, field);
  248. }
  249. kvm_gicd_access(s, offset, cpu, &reg, true);
  250. cpu++;
  251. }
  252. offset += 4;
  253. }
  254. }
  255. static void kvm_arm_gic_put(GICState *s)
  256. {
  257. uint32_t reg;
  258. int i;
  259. int cpu;
  260. int num_cpu;
  261. int num_irq;
  262. /* Note: We do the restore in a slightly different order than the save
  263. * (where the order doesn't matter and is simply ordered according to the
  264. * register offset values */
  265. /*****************************************************************
  266. * Distributor State
  267. */
  268. /* s->ctlr -> GICD_CTLR */
  269. reg = s->ctlr;
  270. kvm_gicd_access(s, 0x0, 0, &reg, true);
  271. /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
  272. kvm_gicd_access(s, 0x4, 0, &reg, false);
  273. num_irq = ((reg & 0x1f) + 1) * 32;
  274. num_cpu = ((reg & 0xe0) >> 5) + 1;
  275. if (num_irq < s->num_irq) {
  276. fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
  277. s->num_irq, num_irq);
  278. abort();
  279. } else if (num_cpu != s->num_cpu) {
  280. fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
  281. s->num_cpu, num_cpu);
  282. /* Did we not create the VCPUs in the kernel yet? */
  283. abort();
  284. }
  285. /* TODO: Consider checking compatibility with the IIDR ? */
  286. /* irq_state[n].enabled -> GICD_ISENABLERn */
  287. kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
  288. kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
  289. /* irq_state[n].group -> GICD_IGROUPRn */
  290. kvm_dist_put(s, 0x80, 1, s->num_irq, translate_group);
  291. /* s->irq_target[irq] -> GICD_ITARGETSRn
  292. * (restore targets before pending to ensure the pending state is set on
  293. * the appropriate CPU interfaces in the kernel) */
  294. kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
  295. /* irq_state[n].trigger -> GICD_ICFGRn
  296. * (restore configuration registers before pending IRQs so we treat
  297. * level/edge correctly) */
  298. kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
  299. /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
  300. kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
  301. kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
  302. /* irq_state[n].active -> GICD_ISACTIVERn */
  303. kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
  304. kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
  305. /* s->priorityX[irq] -> ICD_IPRIORITYRn */
  306. kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
  307. /* s->sgi_pending -> ICD_CPENDSGIRn */
  308. kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
  309. kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);
  310. /*****************************************************************
  311. * CPU Interface(s) State
  312. */
  313. for (cpu = 0; cpu < s->num_cpu; cpu++) {
  314. /* s->cpu_ctlr[cpu] -> GICC_CTLR */
  315. reg = s->cpu_ctlr[cpu];
  316. kvm_gicc_access(s, 0x00, cpu, &reg, true);
  317. /* s->priority_mask[cpu] -> GICC_PMR */
  318. reg = (s->priority_mask[cpu] & 0xff);
  319. kvm_gicc_access(s, 0x04, cpu, &reg, true);
  320. /* s->bpr[cpu] -> GICC_BPR */
  321. reg = (s->bpr[cpu] & 0x7);
  322. kvm_gicc_access(s, 0x08, cpu, &reg, true);
  323. /* s->abpr[cpu] -> GICC_ABPR */
  324. reg = (s->abpr[cpu] & 0x7);
  325. kvm_gicc_access(s, 0x1c, cpu, &reg, true);
  326. /* s->apr[n][cpu] -> GICC_APRn */
  327. for (i = 0; i < 4; i++) {
  328. reg = s->apr[i][cpu];
  329. kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, true);
  330. }
  331. }
  332. }
  333. static void kvm_arm_gic_get(GICState *s)
  334. {
  335. uint32_t reg;
  336. int i;
  337. int cpu;
  338. /*****************************************************************
  339. * Distributor State
  340. */
  341. /* GICD_CTLR -> s->ctlr */
  342. kvm_gicd_access(s, 0x0, 0, &reg, false);
  343. s->ctlr = reg;
  344. /* Sanity checking on GICD_TYPER -> s->num_irq, s->num_cpu */
  345. kvm_gicd_access(s, 0x4, 0, &reg, false);
  346. s->num_irq = ((reg & 0x1f) + 1) * 32;
  347. s->num_cpu = ((reg & 0xe0) >> 5) + 1;
  348. if (s->num_irq > GIC_MAXIRQ) {
  349. fprintf(stderr, "Too many IRQs reported from the kernel: %d\n",
  350. s->num_irq);
  351. abort();
  352. }
  353. /* GICD_IIDR -> ? */
  354. kvm_gicd_access(s, 0x8, 0, &reg, false);
  355. /* Clear all the IRQ settings */
  356. for (i = 0; i < s->num_irq; i++) {
  357. memset(&s->irq_state[i], 0, sizeof(s->irq_state[0]));
  358. }
  359. /* GICD_IGROUPRn -> irq_state[n].group */
  360. kvm_dist_get(s, 0x80, 1, s->num_irq, translate_group);
  361. /* GICD_ISENABLERn -> irq_state[n].enabled */
  362. kvm_dist_get(s, 0x100, 1, s->num_irq, translate_enabled);
  363. /* GICD_ISPENDRn -> irq_state[n].pending + irq_state[n].level */
  364. kvm_dist_get(s, 0x200, 1, s->num_irq, translate_pending);
  365. /* GICD_ISACTIVERn -> irq_state[n].active */
  366. kvm_dist_get(s, 0x300, 1, s->num_irq, translate_active);
  367. /* GICD_ICFRn -> irq_state[n].trigger */
  368. kvm_dist_get(s, 0xc00, 2, s->num_irq, translate_trigger);
  369. /* GICD_IPRIORITYRn -> s->priorityX[irq] */
  370. kvm_dist_get(s, 0x400, 8, s->num_irq, translate_priority);
  371. /* GICD_ITARGETSRn -> s->irq_target[irq] */
  372. kvm_dist_get(s, 0x800, 8, s->num_irq, translate_targets);
  373. /* GICD_CPENDSGIRn -> s->sgi_pending */
  374. kvm_dist_get(s, 0xf10, 8, GIC_NR_SGIS, translate_sgisource);
  375. /*****************************************************************
  376. * CPU Interface(s) State
  377. */
  378. for (cpu = 0; cpu < s->num_cpu; cpu++) {
  379. /* GICC_CTLR -> s->cpu_ctlr[cpu] */
  380. kvm_gicc_access(s, 0x00, cpu, &reg, false);
  381. s->cpu_ctlr[cpu] = reg;
  382. /* GICC_PMR -> s->priority_mask[cpu] */
  383. kvm_gicc_access(s, 0x04, cpu, &reg, false);
  384. s->priority_mask[cpu] = (reg & 0xff);
  385. /* GICC_BPR -> s->bpr[cpu] */
  386. kvm_gicc_access(s, 0x08, cpu, &reg, false);
  387. s->bpr[cpu] = (reg & 0x7);
  388. /* GICC_ABPR -> s->abpr[cpu] */
  389. kvm_gicc_access(s, 0x1c, cpu, &reg, false);
  390. s->abpr[cpu] = (reg & 0x7);
  391. /* GICC_APRn -> s->apr[n][cpu] */
  392. for (i = 0; i < 4; i++) {
  393. kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, false);
  394. s->apr[i][cpu] = reg;
  395. }
  396. }
  397. }
  398. static void kvm_arm_gic_reset_hold(Object *obj, ResetType type)
  399. {
  400. GICState *s = ARM_GIC_COMMON(obj);
  401. KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
  402. if (kgc->parent_phases.hold) {
  403. kgc->parent_phases.hold(obj, type);
  404. }
  405. if (kvm_arm_gic_can_save_restore(s)) {
  406. kvm_arm_gic_put(s);
  407. }
  408. }
  409. static void kvm_arm_gic_realize(DeviceState *dev, Error **errp)
  410. {
  411. int i;
  412. GICState *s = KVM_ARM_GIC(dev);
  413. KVMARMGICClass *kgc = KVM_ARM_GIC_GET_CLASS(s);
  414. Error *local_err = NULL;
  415. int ret;
  416. kgc->parent_realize(dev, &local_err);
  417. if (local_err) {
  418. error_propagate(errp, local_err);
  419. return;
  420. }
  421. if (s->security_extn) {
  422. error_setg(errp, "the in-kernel VGIC does not implement the "
  423. "security extensions");
  424. return;
  425. }
  426. if (s->virt_extn) {
  427. error_setg(errp, "the in-kernel VGIC does not implement the "
  428. "virtualization extensions");
  429. return;
  430. }
  431. if (!kvm_arm_gic_can_save_restore(s)) {
  432. error_setg(&s->migration_blocker, "This operating system kernel does "
  433. "not support vGICv2 migration");
  434. if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
  435. return;
  436. }
  437. }
  438. gic_init_irqs_and_mmio(s, kvm_arm_gicv2_set_irq, NULL, NULL);
  439. for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
  440. qemu_irq irq = qdev_get_gpio_in(dev, i);
  441. kvm_irqchip_set_qemuirq_gsi(kvm_state, irq, i);
  442. }
  443. /* Try to create the device via the device control API */
  444. s->dev_fd = -1;
  445. ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_V2, false);
  446. if (ret >= 0) {
  447. s->dev_fd = ret;
  448. /* Newstyle API is used, we may have attributes */
  449. if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0)) {
  450. uint32_t numirqs = s->num_irq;
  451. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_NR_IRQS, 0,
  452. &numirqs, true, &error_abort);
  453. }
  454. /* Tell the kernel to complete VGIC initialization now */
  455. if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  456. KVM_DEV_ARM_VGIC_CTRL_INIT)) {
  457. kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
  458. KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true,
  459. &error_abort);
  460. }
  461. } else {
  462. error_setg_errno(errp, -ret, "error creating in-kernel VGIC");
  463. error_append_hint(errp,
  464. "Perhaps the host CPU does not support GICv2?\n");
  465. return;
  466. }
  467. /* Distributor */
  468. kvm_arm_register_device(&s->iomem,
  469. (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
  470. | KVM_VGIC_V2_ADDR_TYPE_DIST,
  471. KVM_DEV_ARM_VGIC_GRP_ADDR,
  472. KVM_VGIC_V2_ADDR_TYPE_DIST,
  473. s->dev_fd, 0);
  474. /* CPU interface for current core. Unlike arm_gic, we don't
  475. * provide the "interface for core #N" memory regions, because
  476. * cores with a VGIC don't have those.
  477. */
  478. kvm_arm_register_device(&s->cpuiomem[0],
  479. (KVM_ARM_DEVICE_VGIC_V2 << KVM_ARM_DEVICE_ID_SHIFT)
  480. | KVM_VGIC_V2_ADDR_TYPE_CPU,
  481. KVM_DEV_ARM_VGIC_GRP_ADDR,
  482. KVM_VGIC_V2_ADDR_TYPE_CPU,
  483. s->dev_fd, 0);
  484. if (kvm_has_gsi_routing()) {
  485. /* set up irq routing */
  486. for (i = 0; i < s->num_irq - GIC_INTERNAL; ++i) {
  487. kvm_irqchip_add_irq_route(kvm_state, i, 0, i);
  488. }
  489. kvm_gsi_routing_allowed = true;
  490. kvm_irqchip_commit_routes(kvm_state);
  491. }
  492. }
  493. static void kvm_arm_gic_class_init(ObjectClass *klass, void *data)
  494. {
  495. DeviceClass *dc = DEVICE_CLASS(klass);
  496. ResettableClass *rc = RESETTABLE_CLASS(klass);
  497. ARMGICCommonClass *agcc = ARM_GIC_COMMON_CLASS(klass);
  498. KVMARMGICClass *kgc = KVM_ARM_GIC_CLASS(klass);
  499. agcc->pre_save = kvm_arm_gic_get;
  500. agcc->post_load = kvm_arm_gic_put;
  501. device_class_set_parent_realize(dc, kvm_arm_gic_realize,
  502. &kgc->parent_realize);
  503. resettable_class_set_parent_phases(rc, NULL, kvm_arm_gic_reset_hold, NULL,
  504. &kgc->parent_phases);
  505. }
  506. static const TypeInfo kvm_arm_gic_info = {
  507. .name = TYPE_KVM_ARM_GIC,
  508. .parent = TYPE_ARM_GIC_COMMON,
  509. .instance_size = sizeof(GICState),
  510. .class_init = kvm_arm_gic_class_init,
  511. .class_size = sizeof(KVMARMGICClass),
  512. };
  513. static void kvm_arm_gic_register_types(void)
  514. {
  515. type_register_static(&kvm_arm_gic_info);
  516. }
  517. type_init(kvm_arm_gic_register_types)