cps.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Coherent Processing System emulation.
  3. *
  4. * Copyright (c) 2016 Imagination Technologies
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qapi/error.h"
  21. #include "qemu/module.h"
  22. #include "hw/mips/cps.h"
  23. #include "hw/mips/mips.h"
  24. #include "hw/qdev-clock.h"
  25. #include "hw/qdev-properties.h"
  26. #include "hw/mips/cpudevs.h"
  27. #include "sysemu/kvm.h"
  28. #include "sysemu/reset.h"
  29. qemu_irq get_cps_irq(MIPSCPSState *s, int pin_number)
  30. {
  31. assert(pin_number < s->num_irq);
  32. return s->gic.irq_state[pin_number].irq;
  33. }
  34. static void mips_cps_init(Object *obj)
  35. {
  36. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  37. MIPSCPSState *s = MIPS_CPS(obj);
  38. s->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, NULL, 0);
  39. /*
  40. * Cover entire address space as there do not seem to be any
  41. * constraints for the base address of CPC and GIC.
  42. */
  43. memory_region_init(&s->container, obj, "mips-cps-container", UINT64_MAX);
  44. sysbus_init_mmio(sbd, &s->container);
  45. }
  46. static void main_cpu_reset(void *opaque)
  47. {
  48. MIPSCPU *cpu = opaque;
  49. CPUState *cs = CPU(cpu);
  50. cpu_reset(cs);
  51. }
  52. static bool cpu_mips_itu_supported(CPUMIPSState *env)
  53. {
  54. bool is_mt = (env->CP0_Config5 & (1 << CP0C5_VP)) || ase_mt_available(env);
  55. return is_mt && !kvm_enabled();
  56. }
  57. static void mips_cps_realize(DeviceState *dev, Error **errp)
  58. {
  59. MIPSCPSState *s = MIPS_CPS(dev);
  60. target_ulong gcr_base;
  61. bool itu_present = false;
  62. if (!clock_get(s->clock)) {
  63. error_setg(errp, "CPS input clock is not connected to an output clock");
  64. return;
  65. }
  66. for (int i = 0; i < s->num_vp; i++) {
  67. MIPSCPU *cpu = MIPS_CPU(object_new(s->cpu_type));
  68. CPUMIPSState *env = &cpu->env;
  69. /* All VPs are halted on reset. Leave powering up to CPC. */
  70. if (!object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
  71. errp)) {
  72. return;
  73. }
  74. /* All cores use the same clock tree */
  75. qdev_connect_clock_in(DEVICE(cpu), "clk-in", s->clock);
  76. if (!qdev_realize_and_unref(DEVICE(cpu), NULL, errp)) {
  77. return;
  78. }
  79. /* Init internal devices */
  80. cpu_mips_irq_init_cpu(cpu);
  81. cpu_mips_clock_init(cpu);
  82. if (cpu_mips_itu_supported(env)) {
  83. itu_present = true;
  84. /* Attach ITC Tag to the VP */
  85. env->itc_tag = mips_itu_get_tag_region(&s->itu);
  86. env->itu = &s->itu;
  87. }
  88. qemu_register_reset(main_cpu_reset, cpu);
  89. }
  90. /* Inter-Thread Communication Unit */
  91. if (itu_present) {
  92. object_initialize_child(OBJECT(dev), "itu", &s->itu, TYPE_MIPS_ITU);
  93. object_property_set_link(OBJECT(&s->itu), "cpu[0]",
  94. OBJECT(first_cpu), &error_abort);
  95. object_property_set_uint(OBJECT(&s->itu), "num-fifo", 16,
  96. &error_abort);
  97. object_property_set_uint(OBJECT(&s->itu), "num-semaphores", 16,
  98. &error_abort);
  99. if (!sysbus_realize(SYS_BUS_DEVICE(&s->itu), errp)) {
  100. return;
  101. }
  102. memory_region_add_subregion(&s->container, 0,
  103. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->itu), 0));
  104. }
  105. /* Cluster Power Controller */
  106. object_initialize_child(OBJECT(dev), "cpc", &s->cpc, TYPE_MIPS_CPC);
  107. object_property_set_uint(OBJECT(&s->cpc), "num-vp", s->num_vp,
  108. &error_abort);
  109. object_property_set_int(OBJECT(&s->cpc), "vp-start-running", 1,
  110. &error_abort);
  111. if (!sysbus_realize(SYS_BUS_DEVICE(&s->cpc), errp)) {
  112. return;
  113. }
  114. memory_region_add_subregion(&s->container, 0,
  115. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
  116. /* Global Interrupt Controller */
  117. object_initialize_child(OBJECT(dev), "gic", &s->gic, TYPE_MIPS_GIC);
  118. object_property_set_uint(OBJECT(&s->gic), "num-vp", s->num_vp,
  119. &error_abort);
  120. object_property_set_uint(OBJECT(&s->gic), "num-irq", 128,
  121. &error_abort);
  122. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gic), errp)) {
  123. return;
  124. }
  125. memory_region_add_subregion(&s->container, 0,
  126. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gic), 0));
  127. /* Global Configuration Registers */
  128. gcr_base = MIPS_CPU(first_cpu)->env.CP0_CMGCRBase << 4;
  129. object_initialize_child(OBJECT(dev), "gcr", &s->gcr, TYPE_MIPS_GCR);
  130. object_property_set_uint(OBJECT(&s->gcr), "num-vp", s->num_vp,
  131. &error_abort);
  132. object_property_set_int(OBJECT(&s->gcr), "gcr-rev", 0x800,
  133. &error_abort);
  134. object_property_set_int(OBJECT(&s->gcr), "gcr-base", gcr_base,
  135. &error_abort);
  136. object_property_set_link(OBJECT(&s->gcr), "gic", OBJECT(&s->gic.mr),
  137. &error_abort);
  138. object_property_set_link(OBJECT(&s->gcr), "cpc", OBJECT(&s->cpc.mr),
  139. &error_abort);
  140. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gcr), errp)) {
  141. return;
  142. }
  143. memory_region_add_subregion(&s->container, gcr_base,
  144. sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gcr), 0));
  145. }
  146. static Property mips_cps_properties[] = {
  147. DEFINE_PROP_UINT32("num-vp", MIPSCPSState, num_vp, 1),
  148. DEFINE_PROP_UINT32("num-irq", MIPSCPSState, num_irq, 256),
  149. DEFINE_PROP_STRING("cpu-type", MIPSCPSState, cpu_type),
  150. DEFINE_PROP_END_OF_LIST()
  151. };
  152. static void mips_cps_class_init(ObjectClass *klass, void *data)
  153. {
  154. DeviceClass *dc = DEVICE_CLASS(klass);
  155. dc->realize = mips_cps_realize;
  156. device_class_set_props(dc, mips_cps_properties);
  157. }
  158. static const TypeInfo mips_cps_info = {
  159. .name = TYPE_MIPS_CPS,
  160. .parent = TYPE_SYS_BUS_DEVICE,
  161. .instance_size = sizeof(MIPSCPSState),
  162. .instance_init = mips_cps_init,
  163. .class_init = mips_cps_class_init,
  164. };
  165. static void mips_cps_register_types(void)
  166. {
  167. type_register_static(&mips_cps_info);
  168. }
  169. type_init(mips_cps_register_types)