arm_gic_common.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "arm_gic_internal.h"
  21. static void gic_save(QEMUFile *f, void *opaque)
  22. {
  23. GICState *s = (GICState *)opaque;
  24. int i;
  25. int j;
  26. qemu_put_be32(f, s->enabled);
  27. for (i = 0; i < s->num_cpu; i++) {
  28. qemu_put_be32(f, s->cpu_enabled[i]);
  29. for (j = 0; j < GIC_INTERNAL; j++) {
  30. qemu_put_be32(f, s->priority1[j][i]);
  31. }
  32. for (j = 0; j < s->num_irq; j++) {
  33. qemu_put_be32(f, s->last_active[j][i]);
  34. }
  35. qemu_put_be32(f, s->priority_mask[i]);
  36. qemu_put_be32(f, s->running_irq[i]);
  37. qemu_put_be32(f, s->running_priority[i]);
  38. qemu_put_be32(f, s->current_pending[i]);
  39. }
  40. for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
  41. qemu_put_be32(f, s->priority2[i]);
  42. }
  43. for (i = 0; i < s->num_irq; i++) {
  44. qemu_put_be32(f, s->irq_target[i]);
  45. qemu_put_byte(f, s->irq_state[i].enabled);
  46. qemu_put_byte(f, s->irq_state[i].pending);
  47. qemu_put_byte(f, s->irq_state[i].active);
  48. qemu_put_byte(f, s->irq_state[i].level);
  49. qemu_put_byte(f, s->irq_state[i].model);
  50. qemu_put_byte(f, s->irq_state[i].trigger);
  51. }
  52. }
  53. static int gic_load(QEMUFile *f, void *opaque, int version_id)
  54. {
  55. GICState *s = (GICState *)opaque;
  56. int i;
  57. int j;
  58. if (version_id != 3) {
  59. return -EINVAL;
  60. }
  61. s->enabled = qemu_get_be32(f);
  62. for (i = 0; i < s->num_cpu; i++) {
  63. s->cpu_enabled[i] = qemu_get_be32(f);
  64. for (j = 0; j < GIC_INTERNAL; j++) {
  65. s->priority1[j][i] = qemu_get_be32(f);
  66. }
  67. for (j = 0; j < s->num_irq; j++) {
  68. s->last_active[j][i] = qemu_get_be32(f);
  69. }
  70. s->priority_mask[i] = qemu_get_be32(f);
  71. s->running_irq[i] = qemu_get_be32(f);
  72. s->running_priority[i] = qemu_get_be32(f);
  73. s->current_pending[i] = qemu_get_be32(f);
  74. }
  75. for (i = 0; i < s->num_irq - GIC_INTERNAL; i++) {
  76. s->priority2[i] = qemu_get_be32(f);
  77. }
  78. for (i = 0; i < s->num_irq; i++) {
  79. s->irq_target[i] = qemu_get_be32(f);
  80. s->irq_state[i].enabled = qemu_get_byte(f);
  81. s->irq_state[i].pending = qemu_get_byte(f);
  82. s->irq_state[i].active = qemu_get_byte(f);
  83. s->irq_state[i].level = qemu_get_byte(f);
  84. s->irq_state[i].model = qemu_get_byte(f);
  85. s->irq_state[i].trigger = qemu_get_byte(f);
  86. }
  87. return 0;
  88. }
  89. static int arm_gic_common_init(SysBusDevice *dev)
  90. {
  91. GICState *s = FROM_SYSBUS(GICState, dev);
  92. int num_irq = s->num_irq;
  93. if (s->num_cpu > NCPU) {
  94. hw_error("requested %u CPUs exceeds GIC maximum %d\n",
  95. s->num_cpu, NCPU);
  96. }
  97. s->num_irq += GIC_BASE_IRQ;
  98. if (s->num_irq > GIC_MAXIRQ) {
  99. hw_error("requested %u interrupt lines exceeds GIC maximum %d\n",
  100. num_irq, GIC_MAXIRQ);
  101. }
  102. /* ITLinesNumber is represented as (N / 32) - 1 (see
  103. * gic_dist_readb) so this is an implementation imposed
  104. * restriction, not an architectural one:
  105. */
  106. if (s->num_irq < 32 || (s->num_irq % 32)) {
  107. hw_error("%d interrupt lines unsupported: not divisible by 32\n",
  108. num_irq);
  109. }
  110. register_savevm(NULL, "arm_gic", -1, 3, gic_save, gic_load, s);
  111. return 0;
  112. }
  113. static void arm_gic_common_reset(DeviceState *dev)
  114. {
  115. GICState *s = FROM_SYSBUS(GICState, SYS_BUS_DEVICE(dev));
  116. int i;
  117. memset(s->irq_state, 0, GIC_MAXIRQ * sizeof(gic_irq_state));
  118. for (i = 0 ; i < s->num_cpu; i++) {
  119. if (s->revision == REV_11MPCORE) {
  120. s->priority_mask[i] = 0xf0;
  121. } else {
  122. s->priority_mask[i] = 0;
  123. }
  124. s->current_pending[i] = 1023;
  125. s->running_irq[i] = 1023;
  126. s->running_priority[i] = 0x100;
  127. s->cpu_enabled[i] = 0;
  128. }
  129. for (i = 0; i < 16; i++) {
  130. GIC_SET_ENABLED(i, ALL_CPU_MASK);
  131. GIC_SET_TRIGGER(i);
  132. }
  133. if (s->num_cpu == 1) {
  134. /* For uniprocessor GICs all interrupts always target the sole CPU */
  135. for (i = 0; i < GIC_MAXIRQ; i++) {
  136. s->irq_target[i] = 1;
  137. }
  138. }
  139. s->enabled = 0;
  140. }
  141. static Property arm_gic_common_properties[] = {
  142. DEFINE_PROP_UINT32("num-cpu", GICState, num_cpu, 1),
  143. DEFINE_PROP_UINT32("num-irq", GICState, num_irq, 32),
  144. /* Revision can be 1 or 2 for GIC architecture specification
  145. * versions 1 or 2, or 0 to indicate the legacy 11MPCore GIC.
  146. * (Internally, 0xffffffff also indicates "not a GIC but an NVIC".)
  147. */
  148. DEFINE_PROP_UINT32("revision", GICState, revision, 1),
  149. DEFINE_PROP_END_OF_LIST(),
  150. };
  151. static void arm_gic_common_class_init(ObjectClass *klass, void *data)
  152. {
  153. SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
  154. DeviceClass *dc = DEVICE_CLASS(klass);
  155. dc->reset = arm_gic_common_reset;
  156. dc->props = arm_gic_common_properties;
  157. dc->no_user = 1;
  158. sc->init = arm_gic_common_init;
  159. }
  160. static const TypeInfo arm_gic_common_type = {
  161. .name = TYPE_ARM_GIC_COMMON,
  162. .parent = TYPE_SYS_BUS_DEVICE,
  163. .instance_size = sizeof(GICState),
  164. .class_size = sizeof(ARMGICCommonClass),
  165. .class_init = arm_gic_common_class_init,
  166. .abstract = true,
  167. };
  168. static void register_types(void)
  169. {
  170. type_register_static(&arm_gic_common_type);
  171. }
  172. type_init(register_types)