arm_gicv3_common.c 22 KB

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