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