mips_cpc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Cluster Power Controller 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 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 "cpu.h"
  22. #include "qemu/log.h"
  23. #include "qemu/module.h"
  24. #include "hw/sysbus.h"
  25. #include "migration/vmstate.h"
  26. #include "hw/misc/mips_cpc.h"
  27. #include "hw/qdev-properties.h"
  28. static inline uint64_t cpc_vp_run_mask(MIPSCPCState *cpc)
  29. {
  30. return (1ULL << cpc->num_vp) - 1;
  31. }
  32. static void mips_cpu_reset_async_work(CPUState *cs, run_on_cpu_data data)
  33. {
  34. MIPSCPCState *cpc = (MIPSCPCState *) data.host_ptr;
  35. cpu_reset(cs);
  36. cpc->vp_running |= 1ULL << cs->cpu_index;
  37. }
  38. static void cpc_run_vp(MIPSCPCState *cpc, uint64_t vp_run)
  39. {
  40. CPUState *cs = first_cpu;
  41. CPU_FOREACH(cs) {
  42. uint64_t i = 1ULL << cs->cpu_index;
  43. if (i & vp_run & ~cpc->vp_running) {
  44. /*
  45. * To avoid racing with a CPU we are just kicking off.
  46. * We do the final bit of preparation for the work in
  47. * the target CPUs context.
  48. */
  49. async_safe_run_on_cpu(cs, mips_cpu_reset_async_work,
  50. RUN_ON_CPU_HOST_PTR(cpc));
  51. }
  52. }
  53. }
  54. static void cpc_stop_vp(MIPSCPCState *cpc, uint64_t vp_stop)
  55. {
  56. CPUState *cs = first_cpu;
  57. CPU_FOREACH(cs) {
  58. uint64_t i = 1ULL << cs->cpu_index;
  59. if (i & vp_stop & cpc->vp_running) {
  60. cpu_interrupt(cs, CPU_INTERRUPT_HALT);
  61. cpc->vp_running &= ~i;
  62. }
  63. }
  64. }
  65. static void cpc_write(void *opaque, hwaddr offset, uint64_t data,
  66. unsigned size)
  67. {
  68. MIPSCPCState *s = opaque;
  69. switch (offset) {
  70. case CPC_CL_BASE_OFS + CPC_VP_RUN_OFS:
  71. case CPC_CO_BASE_OFS + CPC_VP_RUN_OFS:
  72. cpc_run_vp(s, data & cpc_vp_run_mask(s));
  73. break;
  74. case CPC_CL_BASE_OFS + CPC_VP_STOP_OFS:
  75. case CPC_CO_BASE_OFS + CPC_VP_STOP_OFS:
  76. cpc_stop_vp(s, data & cpc_vp_run_mask(s));
  77. break;
  78. default:
  79. qemu_log_mask(LOG_UNIMP,
  80. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  81. break;
  82. }
  83. return;
  84. }
  85. static uint64_t cpc_read(void *opaque, hwaddr offset, unsigned size)
  86. {
  87. MIPSCPCState *s = opaque;
  88. switch (offset) {
  89. case CPC_CL_BASE_OFS + CPC_VP_RUNNING_OFS:
  90. case CPC_CO_BASE_OFS + CPC_VP_RUNNING_OFS:
  91. return s->vp_running;
  92. default:
  93. qemu_log_mask(LOG_UNIMP,
  94. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  95. return 0;
  96. }
  97. }
  98. static const MemoryRegionOps cpc_ops = {
  99. .read = cpc_read,
  100. .write = cpc_write,
  101. .endianness = DEVICE_NATIVE_ENDIAN,
  102. .impl = {
  103. .max_access_size = 8,
  104. },
  105. };
  106. static void mips_cpc_init(Object *obj)
  107. {
  108. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  109. MIPSCPCState *s = MIPS_CPC(obj);
  110. memory_region_init_io(&s->mr, OBJECT(s), &cpc_ops, s, "mips-cpc",
  111. CPC_ADDRSPACE_SZ);
  112. sysbus_init_mmio(sbd, &s->mr);
  113. }
  114. static void mips_cpc_realize(DeviceState *dev, Error **errp)
  115. {
  116. MIPSCPCState *s = MIPS_CPC(dev);
  117. if (s->vp_start_running > cpc_vp_run_mask(s)) {
  118. error_setg(errp,
  119. "incorrect vp_start_running 0x%" PRIx64 " for num_vp = %d",
  120. s->vp_running, s->num_vp);
  121. return;
  122. }
  123. }
  124. static void mips_cpc_reset(DeviceState *dev)
  125. {
  126. MIPSCPCState *s = MIPS_CPC(dev);
  127. /* Reflect the fact that all VPs are halted on reset */
  128. s->vp_running = 0;
  129. /* Put selected VPs into run state */
  130. cpc_run_vp(s, s->vp_start_running);
  131. }
  132. static const VMStateDescription vmstate_mips_cpc = {
  133. .name = "mips-cpc",
  134. .version_id = 0,
  135. .minimum_version_id = 0,
  136. .fields = (VMStateField[]) {
  137. VMSTATE_UINT64(vp_running, MIPSCPCState),
  138. VMSTATE_END_OF_LIST()
  139. },
  140. };
  141. static Property mips_cpc_properties[] = {
  142. DEFINE_PROP_UINT32("num-vp", MIPSCPCState, num_vp, 0x1),
  143. DEFINE_PROP_UINT64("vp-start-running", MIPSCPCState, vp_start_running, 0x1),
  144. DEFINE_PROP_END_OF_LIST(),
  145. };
  146. static void mips_cpc_class_init(ObjectClass *klass, void *data)
  147. {
  148. DeviceClass *dc = DEVICE_CLASS(klass);
  149. dc->realize = mips_cpc_realize;
  150. dc->reset = mips_cpc_reset;
  151. dc->vmsd = &vmstate_mips_cpc;
  152. dc->props = mips_cpc_properties;
  153. }
  154. static const TypeInfo mips_cpc_info = {
  155. .name = TYPE_MIPS_CPC,
  156. .parent = TYPE_SYS_BUS_DEVICE,
  157. .instance_size = sizeof(MIPSCPCState),
  158. .instance_init = mips_cpc_init,
  159. .class_init = mips_cpc_class_init,
  160. };
  161. static void mips_cpc_register_types(void)
  162. {
  163. type_register_static(&mips_cpc_info);
  164. }
  165. type_init(mips_cpc_register_types)