2
0

arm_gic_common.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "gic_internal.h"
  21. static void gic_pre_save(void *opaque)
  22. {
  23. GICState *s = (GICState *)opaque;
  24. ARMGICCommonClass *c = ARM_GIC_COMMON_GET_CLASS(s);
  25. if (c->pre_save) {
  26. c->pre_save(s);
  27. }
  28. }
  29. static int gic_post_load(void *opaque, int version_id)
  30. {
  31. GICState *s = (GICState *)opaque;
  32. ARMGICCommonClass *c = ARM_GIC_COMMON_GET_CLASS(s);
  33. if (c->post_load) {
  34. c->post_load(s);
  35. }
  36. return 0;
  37. }
  38. static const VMStateDescription vmstate_gic_irq_state = {
  39. .name = "arm_gic_irq_state",
  40. .version_id = 1,
  41. .minimum_version_id = 1,
  42. .fields = (VMStateField[]) {
  43. VMSTATE_UINT8(enabled, gic_irq_state),
  44. VMSTATE_UINT8(pending, gic_irq_state),
  45. VMSTATE_UINT8(active, gic_irq_state),
  46. VMSTATE_UINT8(level, gic_irq_state),
  47. VMSTATE_BOOL(model, gic_irq_state),
  48. VMSTATE_BOOL(edge_trigger, gic_irq_state),
  49. VMSTATE_END_OF_LIST()
  50. }
  51. };
  52. static const VMStateDescription vmstate_gic = {
  53. .name = "arm_gic",
  54. .version_id = 7,
  55. .minimum_version_id = 7,
  56. .pre_save = gic_pre_save,
  57. .post_load = gic_post_load,
  58. .fields = (VMStateField[]) {
  59. VMSTATE_BOOL(enabled, GICState),
  60. VMSTATE_BOOL_ARRAY(cpu_enabled, GICState, GIC_NCPU),
  61. VMSTATE_STRUCT_ARRAY(irq_state, GICState, GIC_MAXIRQ, 1,
  62. vmstate_gic_irq_state, gic_irq_state),
  63. VMSTATE_UINT8_ARRAY(irq_target, GICState, GIC_MAXIRQ),
  64. VMSTATE_UINT8_2DARRAY(priority1, GICState, GIC_INTERNAL, GIC_NCPU),
  65. VMSTATE_UINT8_ARRAY(priority2, GICState, GIC_MAXIRQ - GIC_INTERNAL),
  66. VMSTATE_UINT16_2DARRAY(last_active, GICState, GIC_MAXIRQ, GIC_NCPU),
  67. VMSTATE_UINT8_2DARRAY(sgi_pending, GICState, GIC_NR_SGIS, GIC_NCPU),
  68. VMSTATE_UINT16_ARRAY(priority_mask, GICState, GIC_NCPU),
  69. VMSTATE_UINT16_ARRAY(running_irq, GICState, GIC_NCPU),
  70. VMSTATE_UINT16_ARRAY(running_priority, GICState, GIC_NCPU),
  71. VMSTATE_UINT16_ARRAY(current_pending, GICState, GIC_NCPU),
  72. VMSTATE_UINT8_ARRAY(bpr, GICState, GIC_NCPU),
  73. VMSTATE_UINT8_ARRAY(abpr, GICState, GIC_NCPU),
  74. VMSTATE_UINT32_2DARRAY(apr, GICState, GIC_NR_APRS, GIC_NCPU),
  75. VMSTATE_END_OF_LIST()
  76. }
  77. };
  78. static void arm_gic_common_realize(DeviceState *dev, Error **errp)
  79. {
  80. GICState *s = ARM_GIC_COMMON(dev);
  81. int num_irq = s->num_irq;
  82. if (s->num_cpu > GIC_NCPU) {
  83. error_setg(errp, "requested %u CPUs exceeds GIC maximum %d",
  84. s->num_cpu, GIC_NCPU);
  85. return;
  86. }
  87. s->num_irq += GIC_BASE_IRQ;
  88. if (s->num_irq > GIC_MAXIRQ) {
  89. error_setg(errp,
  90. "requested %u interrupt lines exceeds GIC maximum %d",
  91. num_irq, GIC_MAXIRQ);
  92. return;
  93. }
  94. /* ITLinesNumber is represented as (N / 32) - 1 (see
  95. * gic_dist_readb) so this is an implementation imposed
  96. * restriction, not an architectural one:
  97. */
  98. if (s->num_irq < 32 || (s->num_irq % 32)) {
  99. error_setg(errp,
  100. "%d interrupt lines unsupported: not divisible by 32",
  101. num_irq);
  102. return;
  103. }
  104. }
  105. static void arm_gic_common_reset(DeviceState *dev)
  106. {
  107. GICState *s = ARM_GIC_COMMON(dev);
  108. int i;
  109. memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
  110. for (i = 0 ; i < s->num_cpu; i++) {
  111. if (s->revision == REV_11MPCORE) {
  112. s->priority_mask[i] = 0xf0;
  113. } else {
  114. s->priority_mask[i] = 0;
  115. }
  116. s->current_pending[i] = 1023;
  117. s->running_irq[i] = 1023;
  118. s->running_priority[i] = 0x100;
  119. s->cpu_enabled[i] = false;
  120. }
  121. for (i = 0; i < 16; i++) {
  122. GIC_SET_ENABLED(i, ALL_CPU_MASK);
  123. GIC_SET_EDGE_TRIGGER(i);
  124. }
  125. if (s->num_cpu == 1) {
  126. /* For uniprocessor GICs all interrupts always target the sole CPU */
  127. for (i = 0; i < GIC_MAXIRQ; i++) {
  128. s->irq_target[i] = 1;
  129. }
  130. }
  131. s->enabled = false;
  132. }
  133. static Property arm_gic_common_properties[] = {
  134. DEFINE_PROP_UINT32("num-cpu", GICState, num_cpu, 1),
  135. DEFINE_PROP_UINT32("num-irq", GICState, num_irq, 32),
  136. /* Revision can be 1 or 2 for GIC architecture specification
  137. * versions 1 or 2, or 0 to indicate the legacy 11MPCore GIC.
  138. * (Internally, 0xffffffff also indicates "not a GIC but an NVIC".)
  139. */
  140. DEFINE_PROP_UINT32("revision", GICState, revision, 1),
  141. DEFINE_PROP_END_OF_LIST(),
  142. };
  143. static void arm_gic_common_class_init(ObjectClass *klass, void *data)
  144. {
  145. DeviceClass *dc = DEVICE_CLASS(klass);
  146. dc->reset = arm_gic_common_reset;
  147. dc->realize = arm_gic_common_realize;
  148. dc->props = arm_gic_common_properties;
  149. dc->vmsd = &vmstate_gic;
  150. }
  151. static const TypeInfo arm_gic_common_type = {
  152. .name = TYPE_ARM_GIC_COMMON,
  153. .parent = TYPE_SYS_BUS_DEVICE,
  154. .instance_size = sizeof(GICState),
  155. .class_size = sizeof(ARMGICCommonClass),
  156. .class_init = arm_gic_common_class_init,
  157. .abstract = true,
  158. };
  159. static void register_types(void)
  160. {
  161. type_register_static(&arm_gic_common_type);
  162. }
  163. type_init(register_types)