allwinner-cpucfg.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Allwinner CPU Configuration Module emulation
  3. *
  4. * Copyright (C) 2019 Niek Linnenbank <nieklinnenbank@gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/timer.h"
  27. #include "hw/core/cpu.h"
  28. #include "target/arm/arm-powerctl.h"
  29. #include "target/arm/cpu.h"
  30. #include "hw/misc/allwinner-cpucfg.h"
  31. #include "trace.h"
  32. /* CPUCFG register offsets */
  33. enum {
  34. REG_CPUS_RST_CTRL = 0x0000, /* CPUs Reset Control */
  35. REG_CPU0_RST_CTRL = 0x0040, /* CPU#0 Reset Control */
  36. REG_CPU0_CTRL = 0x0044, /* CPU#0 Control */
  37. REG_CPU0_STATUS = 0x0048, /* CPU#0 Status */
  38. REG_CPU1_RST_CTRL = 0x0080, /* CPU#1 Reset Control */
  39. REG_CPU1_CTRL = 0x0084, /* CPU#1 Control */
  40. REG_CPU1_STATUS = 0x0088, /* CPU#1 Status */
  41. REG_CPU2_RST_CTRL = 0x00C0, /* CPU#2 Reset Control */
  42. REG_CPU2_CTRL = 0x00C4, /* CPU#2 Control */
  43. REG_CPU2_STATUS = 0x00C8, /* CPU#2 Status */
  44. REG_CPU3_RST_CTRL = 0x0100, /* CPU#3 Reset Control */
  45. REG_CPU3_CTRL = 0x0104, /* CPU#3 Control */
  46. REG_CPU3_STATUS = 0x0108, /* CPU#3 Status */
  47. REG_CPU_SYS_RST = 0x0140, /* CPU System Reset */
  48. REG_CLK_GATING = 0x0144, /* CPU Clock Gating */
  49. REG_GEN_CTRL = 0x0184, /* General Control */
  50. REG_SUPER_STANDBY = 0x01A0, /* Super Standby Flag */
  51. REG_ENTRY_ADDR = 0x01A4, /* Reset Entry Address */
  52. REG_DBG_EXTERN = 0x01E4, /* Debug External */
  53. REG_CNT64_CTRL = 0x0280, /* 64-bit Counter Control */
  54. REG_CNT64_LOW = 0x0284, /* 64-bit Counter Low */
  55. REG_CNT64_HIGH = 0x0288, /* 64-bit Counter High */
  56. };
  57. /* CPUCFG register flags */
  58. enum {
  59. CPUX_RESET_RELEASED = ((1 << 1) | (1 << 0)),
  60. CPUX_STATUS_SMP = (1 << 0),
  61. CPU_SYS_RESET_RELEASED = (1 << 0),
  62. CLK_GATING_ENABLE = ((1 << 8) | 0xF),
  63. };
  64. /* CPUCFG register reset values */
  65. enum {
  66. REG_CLK_GATING_RST = 0x0000010F,
  67. REG_GEN_CTRL_RST = 0x00000020,
  68. REG_SUPER_STANDBY_RST = 0x0,
  69. REG_CNT64_CTRL_RST = 0x0,
  70. };
  71. /* CPUCFG constants */
  72. enum {
  73. CPU_EXCEPTION_LEVEL_ON_RESET = 3, /* EL3 */
  74. };
  75. static void allwinner_cpucfg_cpu_reset(AwCpuCfgState *s, uint8_t cpu_id)
  76. {
  77. int ret;
  78. trace_allwinner_cpucfg_cpu_reset(cpu_id, s->entry_addr);
  79. ARMCPU *target_cpu = ARM_CPU(arm_get_cpu_by_id(cpu_id));
  80. if (!target_cpu) {
  81. /*
  82. * Called with a bogus value for cpu_id. Guest error will
  83. * already have been logged, we can simply return here.
  84. */
  85. return;
  86. }
  87. bool target_aa64 = arm_feature(&target_cpu->env, ARM_FEATURE_AARCH64);
  88. ret = arm_set_cpu_on(cpu_id, s->entry_addr, 0,
  89. CPU_EXCEPTION_LEVEL_ON_RESET, target_aa64);
  90. if (ret != QEMU_ARM_POWERCTL_RET_SUCCESS) {
  91. error_report("%s: failed to bring up CPU %d: err %d",
  92. __func__, cpu_id, ret);
  93. return;
  94. }
  95. }
  96. static uint64_t allwinner_cpucfg_read(void *opaque, hwaddr offset,
  97. unsigned size)
  98. {
  99. const AwCpuCfgState *s = AW_CPUCFG(opaque);
  100. uint64_t val = 0;
  101. switch (offset) {
  102. case REG_CPUS_RST_CTRL: /* CPUs Reset Control */
  103. case REG_CPU_SYS_RST: /* CPU System Reset */
  104. val = CPU_SYS_RESET_RELEASED;
  105. break;
  106. case REG_CPU0_RST_CTRL: /* CPU#0 Reset Control */
  107. case REG_CPU1_RST_CTRL: /* CPU#1 Reset Control */
  108. case REG_CPU2_RST_CTRL: /* CPU#2 Reset Control */
  109. case REG_CPU3_RST_CTRL: /* CPU#3 Reset Control */
  110. val = CPUX_RESET_RELEASED;
  111. break;
  112. case REG_CPU0_CTRL: /* CPU#0 Control */
  113. case REG_CPU1_CTRL: /* CPU#1 Control */
  114. case REG_CPU2_CTRL: /* CPU#2 Control */
  115. case REG_CPU3_CTRL: /* CPU#3 Control */
  116. val = 0;
  117. break;
  118. case REG_CPU0_STATUS: /* CPU#0 Status */
  119. case REG_CPU1_STATUS: /* CPU#1 Status */
  120. case REG_CPU2_STATUS: /* CPU#2 Status */
  121. case REG_CPU3_STATUS: /* CPU#3 Status */
  122. val = CPUX_STATUS_SMP;
  123. break;
  124. case REG_CLK_GATING: /* CPU Clock Gating */
  125. val = CLK_GATING_ENABLE;
  126. break;
  127. case REG_GEN_CTRL: /* General Control */
  128. val = s->gen_ctrl;
  129. break;
  130. case REG_SUPER_STANDBY: /* Super Standby Flag */
  131. val = s->super_standby;
  132. break;
  133. case REG_ENTRY_ADDR: /* Reset Entry Address */
  134. val = s->entry_addr;
  135. break;
  136. case REG_DBG_EXTERN: /* Debug External */
  137. case REG_CNT64_CTRL: /* 64-bit Counter Control */
  138. case REG_CNT64_LOW: /* 64-bit Counter Low */
  139. case REG_CNT64_HIGH: /* 64-bit Counter High */
  140. qemu_log_mask(LOG_UNIMP, "%s: unimplemented register at 0x%04x\n",
  141. __func__, (uint32_t)offset);
  142. break;
  143. default:
  144. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  145. __func__, (uint32_t)offset);
  146. break;
  147. }
  148. trace_allwinner_cpucfg_read(offset, val, size);
  149. return val;
  150. }
  151. static void allwinner_cpucfg_write(void *opaque, hwaddr offset,
  152. uint64_t val, unsigned size)
  153. {
  154. AwCpuCfgState *s = AW_CPUCFG(opaque);
  155. trace_allwinner_cpucfg_write(offset, val, size);
  156. switch (offset) {
  157. case REG_CPUS_RST_CTRL: /* CPUs Reset Control */
  158. case REG_CPU_SYS_RST: /* CPU System Reset */
  159. break;
  160. case REG_CPU0_RST_CTRL: /* CPU#0 Reset Control */
  161. case REG_CPU1_RST_CTRL: /* CPU#1 Reset Control */
  162. case REG_CPU2_RST_CTRL: /* CPU#2 Reset Control */
  163. case REG_CPU3_RST_CTRL: /* CPU#3 Reset Control */
  164. if (val) {
  165. allwinner_cpucfg_cpu_reset(s, (offset - REG_CPU0_RST_CTRL) >> 6);
  166. }
  167. break;
  168. case REG_CPU0_CTRL: /* CPU#0 Control */
  169. case REG_CPU1_CTRL: /* CPU#1 Control */
  170. case REG_CPU2_CTRL: /* CPU#2 Control */
  171. case REG_CPU3_CTRL: /* CPU#3 Control */
  172. case REG_CPU0_STATUS: /* CPU#0 Status */
  173. case REG_CPU1_STATUS: /* CPU#1 Status */
  174. case REG_CPU2_STATUS: /* CPU#2 Status */
  175. case REG_CPU3_STATUS: /* CPU#3 Status */
  176. case REG_CLK_GATING: /* CPU Clock Gating */
  177. break;
  178. case REG_GEN_CTRL: /* General Control */
  179. s->gen_ctrl = val;
  180. break;
  181. case REG_SUPER_STANDBY: /* Super Standby Flag */
  182. s->super_standby = val;
  183. break;
  184. case REG_ENTRY_ADDR: /* Reset Entry Address */
  185. s->entry_addr = val;
  186. break;
  187. case REG_DBG_EXTERN: /* Debug External */
  188. case REG_CNT64_CTRL: /* 64-bit Counter Control */
  189. case REG_CNT64_LOW: /* 64-bit Counter Low */
  190. case REG_CNT64_HIGH: /* 64-bit Counter High */
  191. qemu_log_mask(LOG_UNIMP, "%s: unimplemented register at 0x%04x\n",
  192. __func__, (uint32_t)offset);
  193. break;
  194. default:
  195. qemu_log_mask(LOG_GUEST_ERROR, "%s: out-of-bounds offset 0x%04x\n",
  196. __func__, (uint32_t)offset);
  197. break;
  198. }
  199. }
  200. static const MemoryRegionOps allwinner_cpucfg_ops = {
  201. .read = allwinner_cpucfg_read,
  202. .write = allwinner_cpucfg_write,
  203. .endianness = DEVICE_LITTLE_ENDIAN,
  204. .valid = {
  205. .min_access_size = 4,
  206. .max_access_size = 4,
  207. },
  208. .impl.min_access_size = 4,
  209. };
  210. static void allwinner_cpucfg_reset(DeviceState *dev)
  211. {
  212. AwCpuCfgState *s = AW_CPUCFG(dev);
  213. /* Set default values for registers */
  214. s->gen_ctrl = REG_GEN_CTRL_RST;
  215. s->super_standby = REG_SUPER_STANDBY_RST;
  216. s->entry_addr = 0;
  217. }
  218. static void allwinner_cpucfg_init(Object *obj)
  219. {
  220. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  221. AwCpuCfgState *s = AW_CPUCFG(obj);
  222. /* Memory mapping */
  223. memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_cpucfg_ops, s,
  224. TYPE_AW_CPUCFG, 1 * KiB);
  225. sysbus_init_mmio(sbd, &s->iomem);
  226. }
  227. static const VMStateDescription allwinner_cpucfg_vmstate = {
  228. .name = "allwinner-cpucfg",
  229. .version_id = 1,
  230. .minimum_version_id = 1,
  231. .fields = (const VMStateField[]) {
  232. VMSTATE_UINT32(gen_ctrl, AwCpuCfgState),
  233. VMSTATE_UINT32(super_standby, AwCpuCfgState),
  234. VMSTATE_UINT32(entry_addr, AwCpuCfgState),
  235. VMSTATE_END_OF_LIST()
  236. }
  237. };
  238. static void allwinner_cpucfg_class_init(ObjectClass *klass, void *data)
  239. {
  240. DeviceClass *dc = DEVICE_CLASS(klass);
  241. device_class_set_legacy_reset(dc, allwinner_cpucfg_reset);
  242. dc->vmsd = &allwinner_cpucfg_vmstate;
  243. }
  244. static const TypeInfo allwinner_cpucfg_info = {
  245. .name = TYPE_AW_CPUCFG,
  246. .parent = TYPE_SYS_BUS_DEVICE,
  247. .instance_init = allwinner_cpucfg_init,
  248. .instance_size = sizeof(AwCpuCfgState),
  249. .class_init = allwinner_cpucfg_class_init,
  250. };
  251. static void allwinner_cpucfg_register(void)
  252. {
  253. type_register_static(&allwinner_cpucfg_info);
  254. }
  255. type_init(allwinner_cpucfg_register)