arm_gic_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * ARM GIC support - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2012 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/module.h"
  23. #include "qemu/error-report.h"
  24. #include "gic_internal.h"
  25. #include "hw/arm/linux-boot-if.h"
  26. #include "hw/qdev-properties.h"
  27. #include "migration/vmstate.h"
  28. #include "system/kvm.h"
  29. static int gic_pre_save(void *opaque)
  30. {
  31. GICState *s = (GICState *)opaque;
  32. ARMGICCommonClass *c = ARM_GIC_COMMON_GET_CLASS(s);
  33. if (c->pre_save) {
  34. c->pre_save(s);
  35. }
  36. return 0;
  37. }
  38. static int gic_post_load(void *opaque, int version_id)
  39. {
  40. GICState *s = (GICState *)opaque;
  41. ARMGICCommonClass *c = ARM_GIC_COMMON_GET_CLASS(s);
  42. if (c->post_load) {
  43. c->post_load(s);
  44. }
  45. return 0;
  46. }
  47. static bool gic_virt_state_needed(void *opaque)
  48. {
  49. GICState *s = (GICState *)opaque;
  50. return s->virt_extn;
  51. }
  52. static const VMStateDescription vmstate_gic_irq_state = {
  53. .name = "arm_gic_irq_state",
  54. .version_id = 1,
  55. .minimum_version_id = 1,
  56. .fields = (const VMStateField[]) {
  57. VMSTATE_UINT8(enabled, gic_irq_state),
  58. VMSTATE_UINT8(pending, gic_irq_state),
  59. VMSTATE_UINT8(active, gic_irq_state),
  60. VMSTATE_UINT8(level, gic_irq_state),
  61. VMSTATE_BOOL(model, gic_irq_state),
  62. VMSTATE_BOOL(edge_trigger, gic_irq_state),
  63. VMSTATE_UINT8(group, gic_irq_state),
  64. VMSTATE_END_OF_LIST()
  65. }
  66. };
  67. static const VMStateDescription vmstate_gic_virt_state = {
  68. .name = "arm_gic_virt_state",
  69. .version_id = 1,
  70. .minimum_version_id = 1,
  71. .needed = gic_virt_state_needed,
  72. .fields = (const VMStateField[]) {
  73. /* Virtual interface */
  74. VMSTATE_UINT32_ARRAY(h_hcr, GICState, GIC_NCPU),
  75. VMSTATE_UINT32_ARRAY(h_misr, GICState, GIC_NCPU),
  76. VMSTATE_UINT32_2DARRAY(h_lr, GICState, GIC_MAX_LR, GIC_NCPU),
  77. VMSTATE_UINT32_ARRAY(h_apr, GICState, GIC_NCPU),
  78. /* Virtual CPU interfaces */
  79. VMSTATE_UINT32_SUB_ARRAY(cpu_ctlr, GICState, GIC_NCPU, GIC_NCPU),
  80. VMSTATE_UINT16_SUB_ARRAY(priority_mask, GICState, GIC_NCPU, GIC_NCPU),
  81. VMSTATE_UINT16_SUB_ARRAY(running_priority, GICState, GIC_NCPU, GIC_NCPU),
  82. VMSTATE_UINT16_SUB_ARRAY(current_pending, GICState, GIC_NCPU, GIC_NCPU),
  83. VMSTATE_UINT8_SUB_ARRAY(bpr, GICState, GIC_NCPU, GIC_NCPU),
  84. VMSTATE_UINT8_SUB_ARRAY(abpr, GICState, GIC_NCPU, GIC_NCPU),
  85. VMSTATE_END_OF_LIST()
  86. }
  87. };
  88. static const VMStateDescription vmstate_gic = {
  89. .name = "arm_gic",
  90. .version_id = 12,
  91. .minimum_version_id = 12,
  92. .pre_save = gic_pre_save,
  93. .post_load = gic_post_load,
  94. .fields = (const VMStateField[]) {
  95. VMSTATE_UINT32(ctlr, GICState),
  96. VMSTATE_UINT32_SUB_ARRAY(cpu_ctlr, GICState, 0, GIC_NCPU),
  97. VMSTATE_STRUCT_ARRAY(irq_state, GICState, GIC_MAXIRQ, 1,
  98. vmstate_gic_irq_state, gic_irq_state),
  99. VMSTATE_UINT8_ARRAY(irq_target, GICState, GIC_MAXIRQ),
  100. VMSTATE_UINT8_2DARRAY(priority1, GICState, GIC_INTERNAL, GIC_NCPU),
  101. VMSTATE_UINT8_ARRAY(priority2, GICState, GIC_MAXIRQ - GIC_INTERNAL),
  102. VMSTATE_UINT8_2DARRAY(sgi_pending, GICState, GIC_NR_SGIS, GIC_NCPU),
  103. VMSTATE_UINT16_SUB_ARRAY(priority_mask, GICState, 0, GIC_NCPU),
  104. VMSTATE_UINT16_SUB_ARRAY(running_priority, GICState, 0, GIC_NCPU),
  105. VMSTATE_UINT16_SUB_ARRAY(current_pending, GICState, 0, GIC_NCPU),
  106. VMSTATE_UINT8_SUB_ARRAY(bpr, GICState, 0, GIC_NCPU),
  107. VMSTATE_UINT8_SUB_ARRAY(abpr, GICState, 0, GIC_NCPU),
  108. VMSTATE_UINT32_2DARRAY(apr, GICState, GIC_NR_APRS, GIC_NCPU),
  109. VMSTATE_UINT32_2DARRAY(nsapr, GICState, GIC_NR_APRS, GIC_NCPU),
  110. VMSTATE_END_OF_LIST()
  111. },
  112. .subsections = (const VMStateDescription * const []) {
  113. &vmstate_gic_virt_state,
  114. NULL
  115. }
  116. };
  117. void gic_init_irqs_and_mmio(GICState *s, qemu_irq_handler handler,
  118. const MemoryRegionOps *ops,
  119. const MemoryRegionOps *virt_ops)
  120. {
  121. SysBusDevice *sbd = SYS_BUS_DEVICE(s);
  122. int i = s->num_irq - GIC_INTERNAL;
  123. /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
  124. * GPIO array layout is thus:
  125. * [0..N-1] SPIs
  126. * [N..N+31] PPIs for CPU 0
  127. * [N+32..N+63] PPIs for CPU 1
  128. * ...
  129. */
  130. i += (GIC_INTERNAL * s->num_cpu);
  131. qdev_init_gpio_in(DEVICE(s), handler, i);
  132. for (i = 0; i < s->num_cpu; i++) {
  133. sysbus_init_irq(sbd, &s->parent_irq[i]);
  134. }
  135. for (i = 0; i < s->num_cpu; i++) {
  136. sysbus_init_irq(sbd, &s->parent_fiq[i]);
  137. }
  138. for (i = 0; i < s->num_cpu; i++) {
  139. sysbus_init_irq(sbd, &s->parent_virq[i]);
  140. }
  141. for (i = 0; i < s->num_cpu; i++) {
  142. sysbus_init_irq(sbd, &s->parent_vfiq[i]);
  143. }
  144. if (s->virt_extn) {
  145. for (i = 0; i < s->num_cpu; i++) {
  146. sysbus_init_irq(sbd, &s->maintenance_irq[i]);
  147. }
  148. }
  149. /* Distributor */
  150. memory_region_init_io(&s->iomem, OBJECT(s), ops, s, "gic_dist", 0x1000);
  151. sysbus_init_mmio(sbd, &s->iomem);
  152. /* This is the main CPU interface "for this core". It is always
  153. * present because it is required by both software emulation and KVM.
  154. */
  155. memory_region_init_io(&s->cpuiomem[0], OBJECT(s), ops ? &ops[1] : NULL,
  156. s, "gic_cpu", s->revision == 2 ? 0x2000 : 0x100);
  157. sysbus_init_mmio(sbd, &s->cpuiomem[0]);
  158. if (s->virt_extn) {
  159. memory_region_init_io(&s->vifaceiomem[0], OBJECT(s), virt_ops,
  160. s, "gic_viface", 0x1000);
  161. sysbus_init_mmio(sbd, &s->vifaceiomem[0]);
  162. memory_region_init_io(&s->vcpuiomem, OBJECT(s),
  163. virt_ops ? &virt_ops[1] : NULL,
  164. s, "gic_vcpu", 0x2000);
  165. sysbus_init_mmio(sbd, &s->vcpuiomem);
  166. }
  167. }
  168. static void arm_gic_common_realize(DeviceState *dev, Error **errp)
  169. {
  170. GICState *s = ARM_GIC_COMMON(dev);
  171. int num_irq = s->num_irq;
  172. if (s->num_cpu > GIC_NCPU) {
  173. error_setg(errp, "requested %u CPUs exceeds GIC maximum %d",
  174. s->num_cpu, GIC_NCPU);
  175. return;
  176. }
  177. if (s->num_irq > GIC_MAXIRQ) {
  178. error_setg(errp,
  179. "requested %u interrupt lines exceeds GIC maximum %d",
  180. num_irq, GIC_MAXIRQ);
  181. return;
  182. }
  183. /* ITLinesNumber is represented as (N / 32) - 1 (see
  184. * gic_dist_readb) so this is an implementation imposed
  185. * restriction, not an architectural one:
  186. */
  187. if (s->num_irq < 32 || (s->num_irq % 32)) {
  188. error_setg(errp,
  189. "%d interrupt lines unsupported: not divisible by 32",
  190. num_irq);
  191. return;
  192. }
  193. if (s->security_extn &&
  194. (s->revision == REV_11MPCORE)) {
  195. error_setg(errp, "this GIC revision does not implement "
  196. "the security extensions");
  197. return;
  198. }
  199. if (s->virt_extn) {
  200. if (s->revision != 2) {
  201. error_setg(errp, "GIC virtualization extensions are only "
  202. "supported by revision 2");
  203. return;
  204. }
  205. /* For now, set the number of implemented LRs to 4, as found in most
  206. * real GICv2. This could be promoted as a QOM property if we need to
  207. * emulate a variant with another num_lrs.
  208. */
  209. s->num_lrs = 4;
  210. }
  211. }
  212. static inline void arm_gic_common_reset_irq_state(GICState *s, int cidx,
  213. int resetprio)
  214. {
  215. int i, j;
  216. for (i = cidx; i < cidx + s->num_cpu; i++) {
  217. if (s->revision == REV_11MPCORE) {
  218. s->priority_mask[i] = 0xf0;
  219. } else {
  220. s->priority_mask[i] = resetprio;
  221. }
  222. s->current_pending[i] = 1023;
  223. s->running_priority[i] = 0x100;
  224. s->cpu_ctlr[i] = 0;
  225. s->bpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
  226. s->abpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_ABPR : GIC_MIN_ABPR;
  227. if (!gic_is_vcpu(i)) {
  228. for (j = 0; j < GIC_INTERNAL; j++) {
  229. s->priority1[j][i] = resetprio;
  230. }
  231. for (j = 0; j < GIC_NR_SGIS; j++) {
  232. s->sgi_pending[j][i] = 0;
  233. }
  234. }
  235. }
  236. }
  237. static void arm_gic_common_reset_hold(Object *obj, ResetType type)
  238. {
  239. GICState *s = ARM_GIC_COMMON(obj);
  240. int i, j;
  241. int resetprio;
  242. /* If we're resetting a TZ-aware GIC as if secure firmware
  243. * had set it up ready to start a kernel in non-secure,
  244. * we need to set interrupt priorities to a "zero for the
  245. * NS view" value. This is particularly critical for the
  246. * priority_mask[] values, because if they are zero then NS
  247. * code cannot ever rewrite the priority to anything else.
  248. */
  249. if (s->security_extn && s->irq_reset_nonsecure) {
  250. resetprio = 0x80;
  251. } else {
  252. resetprio = 0;
  253. }
  254. memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
  255. arm_gic_common_reset_irq_state(s, 0, resetprio);
  256. if (s->virt_extn) {
  257. /* vCPU states are stored at indexes GIC_NCPU .. GIC_NCPU+num_cpu.
  258. * The exposed vCPU interface does not have security extensions.
  259. */
  260. arm_gic_common_reset_irq_state(s, GIC_NCPU, 0);
  261. }
  262. for (i = 0; i < GIC_NR_SGIS; i++) {
  263. GIC_DIST_SET_ENABLED(i, ALL_CPU_MASK);
  264. GIC_DIST_SET_EDGE_TRIGGER(i);
  265. }
  266. for (i = 0; i < ARRAY_SIZE(s->priority2); i++) {
  267. s->priority2[i] = resetprio;
  268. }
  269. for (i = 0; i < GIC_MAXIRQ; i++) {
  270. /* For uniprocessor GICs all interrupts always target the sole CPU */
  271. if (s->num_cpu == 1) {
  272. s->irq_target[i] = 1;
  273. } else {
  274. s->irq_target[i] = 0;
  275. }
  276. }
  277. if (s->security_extn && s->irq_reset_nonsecure) {
  278. for (i = 0; i < GIC_MAXIRQ; i++) {
  279. GIC_DIST_SET_GROUP(i, ALL_CPU_MASK);
  280. }
  281. }
  282. if (s->virt_extn) {
  283. for (i = 0; i < s->num_lrs; i++) {
  284. for (j = 0; j < s->num_cpu; j++) {
  285. s->h_lr[i][j] = 0;
  286. }
  287. }
  288. for (i = 0; i < s->num_cpu; i++) {
  289. s->h_hcr[i] = 0;
  290. s->h_misr[i] = 0;
  291. }
  292. }
  293. s->ctlr = 0;
  294. }
  295. static void arm_gic_common_linux_init(ARMLinuxBootIf *obj,
  296. bool secure_boot)
  297. {
  298. GICState *s = ARM_GIC_COMMON(obj);
  299. if (s->security_extn && !secure_boot) {
  300. /* We're directly booting a kernel into NonSecure. If this GIC
  301. * implements the security extensions then we must configure it
  302. * to have all the interrupts be NonSecure (this is a job that
  303. * is done by the Secure boot firmware in real hardware, and in
  304. * this mode QEMU is acting as a minimalist firmware-and-bootloader
  305. * equivalent).
  306. */
  307. s->irq_reset_nonsecure = true;
  308. }
  309. }
  310. static const Property arm_gic_common_properties[] = {
  311. DEFINE_PROP_UINT32("num-cpu", GICState, num_cpu, 1),
  312. DEFINE_PROP_UINT32("num-irq", GICState, num_irq, 32),
  313. /* Revision can be 1 or 2 for GIC architecture specification
  314. * versions 1 or 2, or 0 to indicate the legacy 11MPCore GIC.
  315. */
  316. DEFINE_PROP_UINT32("revision", GICState, revision, 1),
  317. /* True if the GIC should implement the security extensions */
  318. DEFINE_PROP_BOOL("has-security-extensions", GICState, security_extn, 0),
  319. /* True if the GIC should implement the virtualization extensions */
  320. DEFINE_PROP_BOOL("has-virtualization-extensions", GICState, virt_extn, 0),
  321. DEFINE_PROP_UINT32("num-priority-bits", GICState, n_prio_bits, 8),
  322. };
  323. static void arm_gic_common_class_init(ObjectClass *klass, void *data)
  324. {
  325. DeviceClass *dc = DEVICE_CLASS(klass);
  326. ResettableClass *rc = RESETTABLE_CLASS(klass);
  327. ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_CLASS(klass);
  328. rc->phases.hold = arm_gic_common_reset_hold;
  329. dc->realize = arm_gic_common_realize;
  330. device_class_set_props(dc, arm_gic_common_properties);
  331. dc->vmsd = &vmstate_gic;
  332. albifc->arm_linux_init = arm_gic_common_linux_init;
  333. }
  334. static const TypeInfo arm_gic_common_type = {
  335. .name = TYPE_ARM_GIC_COMMON,
  336. .parent = TYPE_SYS_BUS_DEVICE,
  337. .instance_size = sizeof(GICState),
  338. .class_size = sizeof(ARMGICCommonClass),
  339. .class_init = arm_gic_common_class_init,
  340. .abstract = true,
  341. .interfaces = (InterfaceInfo []) {
  342. { TYPE_ARM_LINUX_BOOT_IF },
  343. { },
  344. },
  345. };
  346. static void register_types(void)
  347. {
  348. type_register_static(&arm_gic_common_type);
  349. }
  350. type_init(register_types)
  351. const char *gic_class_name(void)
  352. {
  353. return kvm_irqchip_in_kernel() ? "kvm-arm-gic" : "arm_gic";
  354. }