arm_gicv3_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * ARM GICv3 support - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2012 Linaro Limited
  5. * Copyright (c) 2015 Huawei.
  6. * Copyright (c) 2015 Samsung Electronics Co., Ltd.
  7. * Written by Peter Maydell
  8. * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qapi/error.h"
  25. #include "qemu/module.h"
  26. #include "hw/core/cpu.h"
  27. #include "hw/intc/arm_gicv3_common.h"
  28. #include "hw/qdev-properties.h"
  29. #include "migration/vmstate.h"
  30. #include "gicv3_internal.h"
  31. #include "hw/arm/linux-boot-if.h"
  32. #include "sysemu/kvm.h"
  33. static void gicv3_gicd_no_migration_shift_bug_post_load(GICv3State *cs)
  34. {
  35. if (cs->gicd_no_migration_shift_bug) {
  36. return;
  37. }
  38. /* Older versions of QEMU had a bug in the handling of state save/restore
  39. * to the KVM GICv3: they got the offset in the bitmap arrays wrong,
  40. * so that instead of the data for external interrupts 32 and up
  41. * starting at bit position 32 in the bitmap, it started at bit
  42. * position 64. If we're receiving data from a QEMU with that bug,
  43. * we must move the data down into the right place.
  44. */
  45. memmove(cs->group, (uint8_t *)cs->group + GIC_INTERNAL / 8,
  46. sizeof(cs->group) - GIC_INTERNAL / 8);
  47. memmove(cs->grpmod, (uint8_t *)cs->grpmod + GIC_INTERNAL / 8,
  48. sizeof(cs->grpmod) - GIC_INTERNAL / 8);
  49. memmove(cs->enabled, (uint8_t *)cs->enabled + GIC_INTERNAL / 8,
  50. sizeof(cs->enabled) - GIC_INTERNAL / 8);
  51. memmove(cs->pending, (uint8_t *)cs->pending + GIC_INTERNAL / 8,
  52. sizeof(cs->pending) - GIC_INTERNAL / 8);
  53. memmove(cs->active, (uint8_t *)cs->active + GIC_INTERNAL / 8,
  54. sizeof(cs->active) - GIC_INTERNAL / 8);
  55. memmove(cs->edge_trigger, (uint8_t *)cs->edge_trigger + GIC_INTERNAL / 8,
  56. sizeof(cs->edge_trigger) - GIC_INTERNAL / 8);
  57. /*
  58. * While this new version QEMU doesn't have this kind of bug as we fix it,
  59. * so it needs to set the flag to true to indicate that and it's necessary
  60. * for next migration to work from this new version QEMU.
  61. */
  62. cs->gicd_no_migration_shift_bug = true;
  63. }
  64. static int gicv3_pre_save(void *opaque)
  65. {
  66. GICv3State *s = (GICv3State *)opaque;
  67. ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
  68. if (c->pre_save) {
  69. c->pre_save(s);
  70. }
  71. return 0;
  72. }
  73. static int gicv3_post_load(void *opaque, int version_id)
  74. {
  75. GICv3State *s = (GICv3State *)opaque;
  76. ARMGICv3CommonClass *c = ARM_GICV3_COMMON_GET_CLASS(s);
  77. gicv3_gicd_no_migration_shift_bug_post_load(s);
  78. if (c->post_load) {
  79. c->post_load(s);
  80. }
  81. return 0;
  82. }
  83. static bool virt_state_needed(void *opaque)
  84. {
  85. GICv3CPUState *cs = opaque;
  86. return cs->num_list_regs != 0;
  87. }
  88. static const VMStateDescription vmstate_gicv3_cpu_virt = {
  89. .name = "arm_gicv3_cpu/virt",
  90. .version_id = 1,
  91. .minimum_version_id = 1,
  92. .needed = virt_state_needed,
  93. .fields = (VMStateField[]) {
  94. VMSTATE_UINT64_2DARRAY(ich_apr, GICv3CPUState, 3, 4),
  95. VMSTATE_UINT64(ich_hcr_el2, GICv3CPUState),
  96. VMSTATE_UINT64_ARRAY(ich_lr_el2, GICv3CPUState, GICV3_LR_MAX),
  97. VMSTATE_UINT64(ich_vmcr_el2, GICv3CPUState),
  98. VMSTATE_END_OF_LIST()
  99. }
  100. };
  101. static int vmstate_gicv3_cpu_pre_load(void *opaque)
  102. {
  103. GICv3CPUState *cs = opaque;
  104. /*
  105. * If the sre_el1 subsection is not transferred this
  106. * means SRE_EL1 is 0x7 (which might not be the same as
  107. * our reset value).
  108. */
  109. cs->icc_sre_el1 = 0x7;
  110. return 0;
  111. }
  112. static bool icc_sre_el1_reg_needed(void *opaque)
  113. {
  114. GICv3CPUState *cs = opaque;
  115. return cs->icc_sre_el1 != 7;
  116. }
  117. const VMStateDescription vmstate_gicv3_cpu_sre_el1 = {
  118. .name = "arm_gicv3_cpu/sre_el1",
  119. .version_id = 1,
  120. .minimum_version_id = 1,
  121. .needed = icc_sre_el1_reg_needed,
  122. .fields = (VMStateField[]) {
  123. VMSTATE_UINT64(icc_sre_el1, GICv3CPUState),
  124. VMSTATE_END_OF_LIST()
  125. }
  126. };
  127. static bool gicv4_needed(void *opaque)
  128. {
  129. GICv3CPUState *cs = opaque;
  130. return cs->gic->revision > 3;
  131. }
  132. const VMStateDescription vmstate_gicv3_gicv4 = {
  133. .name = "arm_gicv3_cpu/gicv4",
  134. .version_id = 1,
  135. .minimum_version_id = 1,
  136. .needed = gicv4_needed,
  137. .fields = (VMStateField[]) {
  138. VMSTATE_UINT64(gicr_vpropbaser, GICv3CPUState),
  139. VMSTATE_UINT64(gicr_vpendbaser, GICv3CPUState),
  140. VMSTATE_END_OF_LIST()
  141. }
  142. };
  143. static const VMStateDescription vmstate_gicv3_cpu = {
  144. .name = "arm_gicv3_cpu",
  145. .version_id = 1,
  146. .minimum_version_id = 1,
  147. .pre_load = vmstate_gicv3_cpu_pre_load,
  148. .fields = (VMStateField[]) {
  149. VMSTATE_UINT32(level, GICv3CPUState),
  150. VMSTATE_UINT32(gicr_ctlr, GICv3CPUState),
  151. VMSTATE_UINT32_ARRAY(gicr_statusr, GICv3CPUState, 2),
  152. VMSTATE_UINT32(gicr_waker, GICv3CPUState),
  153. VMSTATE_UINT64(gicr_propbaser, GICv3CPUState),
  154. VMSTATE_UINT64(gicr_pendbaser, GICv3CPUState),
  155. VMSTATE_UINT32(gicr_igroupr0, GICv3CPUState),
  156. VMSTATE_UINT32(gicr_ienabler0, GICv3CPUState),
  157. VMSTATE_UINT32(gicr_ipendr0, GICv3CPUState),
  158. VMSTATE_UINT32(gicr_iactiver0, GICv3CPUState),
  159. VMSTATE_UINT32(edge_trigger, GICv3CPUState),
  160. VMSTATE_UINT32(gicr_igrpmodr0, GICv3CPUState),
  161. VMSTATE_UINT32(gicr_nsacr, GICv3CPUState),
  162. VMSTATE_UINT8_ARRAY(gicr_ipriorityr, GICv3CPUState, GIC_INTERNAL),
  163. VMSTATE_UINT64_ARRAY(icc_ctlr_el1, GICv3CPUState, 2),
  164. VMSTATE_UINT64(icc_pmr_el1, GICv3CPUState),
  165. VMSTATE_UINT64_ARRAY(icc_bpr, GICv3CPUState, 3),
  166. VMSTATE_UINT64_2DARRAY(icc_apr, GICv3CPUState, 3, 4),
  167. VMSTATE_UINT64_ARRAY(icc_igrpen, GICv3CPUState, 3),
  168. VMSTATE_UINT64(icc_ctlr_el3, GICv3CPUState),
  169. VMSTATE_END_OF_LIST()
  170. },
  171. .subsections = (const VMStateDescription * []) {
  172. &vmstate_gicv3_cpu_virt,
  173. &vmstate_gicv3_cpu_sre_el1,
  174. &vmstate_gicv3_gicv4,
  175. NULL
  176. }
  177. };
  178. static int gicv3_pre_load(void *opaque)
  179. {
  180. GICv3State *cs = opaque;
  181. /*
  182. * The gicd_no_migration_shift_bug flag is used for migration compatibility
  183. * for old version QEMU which may have the GICD bmp shift bug under KVM mode.
  184. * Strictly, what we want to know is whether the migration source is using
  185. * KVM. Since we don't have any way to determine that, we look at whether the
  186. * destination is using KVM; this is close enough because for the older QEMU
  187. * versions with this bug KVM -> TCG migration didn't work anyway. If the
  188. * source is a newer QEMU without this bug it will transmit the migration
  189. * subsection which sets the flag to true; otherwise it will remain set to
  190. * the value we select here.
  191. */
  192. if (kvm_enabled()) {
  193. cs->gicd_no_migration_shift_bug = false;
  194. }
  195. return 0;
  196. }
  197. static bool needed_always(void *opaque)
  198. {
  199. return true;
  200. }
  201. const VMStateDescription vmstate_gicv3_gicd_no_migration_shift_bug = {
  202. .name = "arm_gicv3/gicd_no_migration_shift_bug",
  203. .version_id = 1,
  204. .minimum_version_id = 1,
  205. .needed = needed_always,
  206. .fields = (VMStateField[]) {
  207. VMSTATE_BOOL(gicd_no_migration_shift_bug, GICv3State),
  208. VMSTATE_END_OF_LIST()
  209. }
  210. };
  211. static const VMStateDescription vmstate_gicv3 = {
  212. .name = "arm_gicv3",
  213. .version_id = 1,
  214. .minimum_version_id = 1,
  215. .pre_load = gicv3_pre_load,
  216. .pre_save = gicv3_pre_save,
  217. .post_load = gicv3_post_load,
  218. .priority = MIG_PRI_GICV3,
  219. .fields = (VMStateField[]) {
  220. VMSTATE_UINT32(gicd_ctlr, GICv3State),
  221. VMSTATE_UINT32_ARRAY(gicd_statusr, GICv3State, 2),
  222. VMSTATE_UINT32_ARRAY(group, GICv3State, GICV3_BMP_SIZE),
  223. VMSTATE_UINT32_ARRAY(grpmod, GICv3State, GICV3_BMP_SIZE),
  224. VMSTATE_UINT32_ARRAY(enabled, GICv3State, GICV3_BMP_SIZE),
  225. VMSTATE_UINT32_ARRAY(pending, GICv3State, GICV3_BMP_SIZE),
  226. VMSTATE_UINT32_ARRAY(active, GICv3State, GICV3_BMP_SIZE),
  227. VMSTATE_UINT32_ARRAY(level, GICv3State, GICV3_BMP_SIZE),
  228. VMSTATE_UINT32_ARRAY(edge_trigger, GICv3State, GICV3_BMP_SIZE),
  229. VMSTATE_UINT8_ARRAY(gicd_ipriority, GICv3State, GICV3_MAXIRQ),
  230. VMSTATE_UINT64_ARRAY(gicd_irouter, GICv3State, GICV3_MAXIRQ),
  231. VMSTATE_UINT32_ARRAY(gicd_nsacr, GICv3State,
  232. DIV_ROUND_UP(GICV3_MAXIRQ, 16)),
  233. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(cpu, GICv3State, num_cpu,
  234. vmstate_gicv3_cpu, GICv3CPUState),
  235. VMSTATE_END_OF_LIST()
  236. },
  237. .subsections = (const VMStateDescription * []) {
  238. &vmstate_gicv3_gicd_no_migration_shift_bug,
  239. NULL
  240. }
  241. };
  242. void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
  243. const MemoryRegionOps *ops)
  244. {
  245. SysBusDevice *sbd = SYS_BUS_DEVICE(s);
  246. int i;
  247. int cpuidx;
  248. /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
  249. * GPIO array layout is thus:
  250. * [0..N-1] spi
  251. * [N..N+31] PPIs for CPU 0
  252. * [N+32..N+63] PPIs for CPU 1
  253. * ...
  254. */
  255. i = s->num_irq - GIC_INTERNAL + GIC_INTERNAL * s->num_cpu;
  256. qdev_init_gpio_in(DEVICE(s), handler, i);
  257. for (i = 0; i < s->num_cpu; i++) {
  258. sysbus_init_irq(sbd, &s->cpu[i].parent_irq);
  259. }
  260. for (i = 0; i < s->num_cpu; i++) {
  261. sysbus_init_irq(sbd, &s->cpu[i].parent_fiq);
  262. }
  263. for (i = 0; i < s->num_cpu; i++) {
  264. sysbus_init_irq(sbd, &s->cpu[i].parent_virq);
  265. }
  266. for (i = 0; i < s->num_cpu; i++) {
  267. sysbus_init_irq(sbd, &s->cpu[i].parent_vfiq);
  268. }
  269. memory_region_init_io(&s->iomem_dist, OBJECT(s), ops, s,
  270. "gicv3_dist", 0x10000);
  271. sysbus_init_mmio(sbd, &s->iomem_dist);
  272. s->redist_regions = g_new0(GICv3RedistRegion, s->nb_redist_regions);
  273. cpuidx = 0;
  274. for (i = 0; i < s->nb_redist_regions; i++) {
  275. char *name = g_strdup_printf("gicv3_redist_region[%d]", i);
  276. GICv3RedistRegion *region = &s->redist_regions[i];
  277. region->gic = s;
  278. region->cpuidx = cpuidx;
  279. cpuidx += s->redist_region_count[i];
  280. memory_region_init_io(&region->iomem, OBJECT(s),
  281. ops ? &ops[1] : NULL, region, name,
  282. s->redist_region_count[i] * gicv3_redist_size(s));
  283. sysbus_init_mmio(sbd, &region->iomem);
  284. g_free(name);
  285. }
  286. }
  287. static void arm_gicv3_common_realize(DeviceState *dev, Error **errp)
  288. {
  289. GICv3State *s = ARM_GICV3_COMMON(dev);
  290. int i, rdist_capacity, cpuidx;
  291. /*
  292. * This GIC device supports only revisions 3 and 4. The GICv1/v2
  293. * is a separate device.
  294. * Note that subclasses of this device may impose further restrictions
  295. * on the GIC revision: notably, the in-kernel KVM GIC doesn't
  296. * support GICv4.
  297. */
  298. if (s->revision != 3 && s->revision != 4) {
  299. error_setg(errp, "unsupported GIC revision %d", s->revision);
  300. return;
  301. }
  302. if (s->num_irq > GICV3_MAXIRQ) {
  303. error_setg(errp,
  304. "requested %u interrupt lines exceeds GIC maximum %d",
  305. s->num_irq, GICV3_MAXIRQ);
  306. return;
  307. }
  308. if (s->num_irq < GIC_INTERNAL) {
  309. error_setg(errp,
  310. "requested %u interrupt lines is below GIC minimum %d",
  311. s->num_irq, GIC_INTERNAL);
  312. return;
  313. }
  314. if (s->num_cpu == 0) {
  315. error_setg(errp, "num-cpu must be at least 1");
  316. return;
  317. }
  318. /* ITLinesNumber is represented as (N / 32) - 1, so this is an
  319. * implementation imposed restriction, not an architectural one,
  320. * so we don't have to deal with bitfields where only some of the
  321. * bits in a 32-bit word should be valid.
  322. */
  323. if (s->num_irq % 32) {
  324. error_setg(errp,
  325. "%d interrupt lines unsupported: not divisible by 32",
  326. s->num_irq);
  327. return;
  328. }
  329. if (s->lpi_enable && !s->dma) {
  330. error_setg(errp, "Redist-ITS: Guest 'sysmem' reference link not set");
  331. return;
  332. }
  333. rdist_capacity = 0;
  334. for (i = 0; i < s->nb_redist_regions; i++) {
  335. rdist_capacity += s->redist_region_count[i];
  336. }
  337. if (rdist_capacity != s->num_cpu) {
  338. error_setg(errp, "Capacity of the redist regions(%d) "
  339. "does not match the number of vcpus(%d)",
  340. rdist_capacity, s->num_cpu);
  341. return;
  342. }
  343. if (s->lpi_enable) {
  344. address_space_init(&s->dma_as, s->dma,
  345. "gicv3-its-sysmem");
  346. }
  347. s->cpu = g_new0(GICv3CPUState, s->num_cpu);
  348. for (i = 0; i < s->num_cpu; i++) {
  349. CPUState *cpu = qemu_get_cpu(i);
  350. uint64_t cpu_affid;
  351. s->cpu[i].cpu = cpu;
  352. s->cpu[i].gic = s;
  353. /* Store GICv3CPUState in CPUARMState gicv3state pointer */
  354. gicv3_set_gicv3state(cpu, &s->cpu[i]);
  355. /* Pre-construct the GICR_TYPER:
  356. * For our implementation:
  357. * Top 32 bits are the affinity value of the associated CPU
  358. * CommonLPIAff == 01 (redistributors with same Aff3 share LPI table)
  359. * Processor_Number == CPU index starting from 0
  360. * DPGS == 0 (GICR_CTLR.DPG* not supported)
  361. * Last == 1 if this is the last redistributor in a series of
  362. * contiguous redistributor pages
  363. * DirectLPI == 0 (direct injection of LPIs not supported)
  364. * VLPIS == 1 if vLPIs supported (GICv4 and up)
  365. * PLPIS == 1 if LPIs supported
  366. */
  367. cpu_affid = object_property_get_uint(OBJECT(cpu), "mp-affinity", NULL);
  368. /* The CPU mp-affinity property is in MPIDR register format; squash
  369. * the affinity bytes into 32 bits as the GICR_TYPER has them.
  370. */
  371. cpu_affid = ((cpu_affid & 0xFF00000000ULL) >> 8) |
  372. (cpu_affid & 0xFFFFFF);
  373. s->cpu[i].gicr_typer = (cpu_affid << 32) |
  374. (1 << 24) |
  375. (i << 8);
  376. if (s->lpi_enable) {
  377. s->cpu[i].gicr_typer |= GICR_TYPER_PLPIS;
  378. if (s->revision > 3) {
  379. s->cpu[i].gicr_typer |= GICR_TYPER_VLPIS;
  380. }
  381. }
  382. }
  383. /*
  384. * Now go through and set GICR_TYPER.Last for the final
  385. * redistributor in each region.
  386. */
  387. cpuidx = 0;
  388. for (i = 0; i < s->nb_redist_regions; i++) {
  389. cpuidx += s->redist_region_count[i];
  390. s->cpu[cpuidx - 1].gicr_typer |= GICR_TYPER_LAST;
  391. }
  392. s->itslist = g_ptr_array_new();
  393. }
  394. static void arm_gicv3_finalize(Object *obj)
  395. {
  396. GICv3State *s = ARM_GICV3_COMMON(obj);
  397. g_free(s->redist_region_count);
  398. }
  399. static void arm_gicv3_common_reset_hold(Object *obj)
  400. {
  401. GICv3State *s = ARM_GICV3_COMMON(obj);
  402. int i;
  403. for (i = 0; i < s->num_cpu; i++) {
  404. GICv3CPUState *cs = &s->cpu[i];
  405. cs->level = 0;
  406. cs->gicr_ctlr = 0;
  407. if (s->lpi_enable) {
  408. /* Our implementation supports clearing GICR_CTLR.EnableLPIs */
  409. cs->gicr_ctlr |= GICR_CTLR_CES;
  410. }
  411. cs->gicr_statusr[GICV3_S] = 0;
  412. cs->gicr_statusr[GICV3_NS] = 0;
  413. cs->gicr_waker = GICR_WAKER_ProcessorSleep | GICR_WAKER_ChildrenAsleep;
  414. cs->gicr_propbaser = 0;
  415. cs->gicr_pendbaser = 0;
  416. cs->gicr_vpropbaser = 0;
  417. cs->gicr_vpendbaser = 0;
  418. /* If we're resetting a TZ-aware GIC as if secure firmware
  419. * had set it up ready to start a kernel in non-secure, we
  420. * need to set interrupts to group 1 so the kernel can use them.
  421. * Otherwise they reset to group 0 like the hardware.
  422. */
  423. if (s->irq_reset_nonsecure) {
  424. cs->gicr_igroupr0 = 0xffffffff;
  425. } else {
  426. cs->gicr_igroupr0 = 0;
  427. }
  428. cs->gicr_ienabler0 = 0;
  429. cs->gicr_ipendr0 = 0;
  430. cs->gicr_iactiver0 = 0;
  431. cs->edge_trigger = 0xffff;
  432. cs->gicr_igrpmodr0 = 0;
  433. cs->gicr_nsacr = 0;
  434. memset(cs->gicr_ipriorityr, 0, sizeof(cs->gicr_ipriorityr));
  435. cs->hppi.prio = 0xff;
  436. cs->hpplpi.prio = 0xff;
  437. cs->hppvlpi.prio = 0xff;
  438. /* State in the CPU interface must *not* be reset here, because it
  439. * is part of the CPU's reset domain, not the GIC device's.
  440. */
  441. }
  442. /* For our implementation affinity routing is always enabled */
  443. if (s->security_extn) {
  444. s->gicd_ctlr = GICD_CTLR_ARE_S | GICD_CTLR_ARE_NS;
  445. } else {
  446. s->gicd_ctlr = GICD_CTLR_DS | GICD_CTLR_ARE;
  447. }
  448. s->gicd_statusr[GICV3_S] = 0;
  449. s->gicd_statusr[GICV3_NS] = 0;
  450. memset(s->group, 0, sizeof(s->group));
  451. memset(s->grpmod, 0, sizeof(s->grpmod));
  452. memset(s->enabled, 0, sizeof(s->enabled));
  453. memset(s->pending, 0, sizeof(s->pending));
  454. memset(s->active, 0, sizeof(s->active));
  455. memset(s->level, 0, sizeof(s->level));
  456. memset(s->edge_trigger, 0, sizeof(s->edge_trigger));
  457. memset(s->gicd_ipriority, 0, sizeof(s->gicd_ipriority));
  458. memset(s->gicd_irouter, 0, sizeof(s->gicd_irouter));
  459. memset(s->gicd_nsacr, 0, sizeof(s->gicd_nsacr));
  460. /* GICD_IROUTER are UNKNOWN at reset so in theory the guest must
  461. * write these to get sane behaviour and we need not populate the
  462. * pointer cache here; however having the cache be different for
  463. * "happened to be 0 from reset" and "guest wrote 0" would be
  464. * too confusing.
  465. */
  466. gicv3_cache_all_target_cpustates(s);
  467. if (s->irq_reset_nonsecure) {
  468. /* If we're resetting a TZ-aware GIC as if secure firmware
  469. * had set it up ready to start a kernel in non-secure, we
  470. * need to set interrupts to group 1 so the kernel can use them.
  471. * Otherwise they reset to group 0 like the hardware.
  472. */
  473. for (i = GIC_INTERNAL; i < s->num_irq; i++) {
  474. gicv3_gicd_group_set(s, i);
  475. }
  476. }
  477. s->gicd_no_migration_shift_bug = true;
  478. }
  479. static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
  480. bool secure_boot)
  481. {
  482. GICv3State *s = ARM_GICV3_COMMON(obj);
  483. if (s->security_extn && !secure_boot) {
  484. /* We're directly booting a kernel into NonSecure. If this GIC
  485. * implements the security extensions then we must configure it
  486. * to have all the interrupts be NonSecure (this is a job that
  487. * is done by the Secure boot firmware in real hardware, and in
  488. * this mode QEMU is acting as a minimalist firmware-and-bootloader
  489. * equivalent).
  490. */
  491. s->irq_reset_nonsecure = true;
  492. }
  493. }
  494. static Property arm_gicv3_common_properties[] = {
  495. DEFINE_PROP_UINT32("num-cpu", GICv3State, num_cpu, 1),
  496. DEFINE_PROP_UINT32("num-irq", GICv3State, num_irq, 32),
  497. DEFINE_PROP_UINT32("revision", GICv3State, revision, 3),
  498. DEFINE_PROP_BOOL("has-lpi", GICv3State, lpi_enable, 0),
  499. DEFINE_PROP_BOOL("has-security-extensions", GICv3State, security_extn, 0),
  500. /*
  501. * Compatibility property: force 8 bits of physical priority, even
  502. * if the CPU being emulated should have fewer.
  503. */
  504. DEFINE_PROP_BOOL("force-8-bit-prio", GICv3State, force_8bit_prio, 0),
  505. DEFINE_PROP_ARRAY("redist-region-count", GICv3State, nb_redist_regions,
  506. redist_region_count, qdev_prop_uint32, uint32_t),
  507. DEFINE_PROP_LINK("sysmem", GICv3State, dma, TYPE_MEMORY_REGION,
  508. MemoryRegion *),
  509. DEFINE_PROP_END_OF_LIST(),
  510. };
  511. static void arm_gicv3_common_class_init(ObjectClass *klass, void *data)
  512. {
  513. DeviceClass *dc = DEVICE_CLASS(klass);
  514. ResettableClass *rc = RESETTABLE_CLASS(klass);
  515. ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
  516. rc->phases.hold = arm_gicv3_common_reset_hold;
  517. dc->realize = arm_gicv3_common_realize;
  518. device_class_set_props(dc, arm_gicv3_common_properties);
  519. dc->vmsd = &vmstate_gicv3;
  520. albifc->arm_linux_init = arm_gic_common_linux_init;
  521. }
  522. static const TypeInfo arm_gicv3_common_type = {
  523. .name = TYPE_ARM_GICV3_COMMON,
  524. .parent = TYPE_SYS_BUS_DEVICE,
  525. .instance_size = sizeof(GICv3State),
  526. .class_size = sizeof(ARMGICv3CommonClass),
  527. .class_init = arm_gicv3_common_class_init,
  528. .instance_finalize = arm_gicv3_finalize,
  529. .abstract = true,
  530. .interfaces = (InterfaceInfo []) {
  531. { TYPE_ARM_LINUX_BOOT_IF },
  532. { },
  533. },
  534. };
  535. static void register_types(void)
  536. {
  537. type_register_static(&arm_gicv3_common_type);
  538. }
  539. type_init(register_types)