mips_cpc.c 4.7 KB

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