ppce500_spin.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * QEMU PowerPC e500v2 ePAPR spinning code
  3. *
  4. * Copyright (C) 2011 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author: Alexander Graf, <agraf@suse.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * This code is not really a device, but models an interface that usually
  22. * firmware takes care of. It's used when QEMU plays the role of firmware.
  23. *
  24. * Specification:
  25. *
  26. * https://www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.1.pdf
  27. *
  28. */
  29. #include "qemu/osdep.h"
  30. #include "qemu/module.h"
  31. #include "qemu/units.h"
  32. #include "hw/hw.h"
  33. #include "hw/sysbus.h"
  34. #include "system/hw_accel.h"
  35. #include "hw/ppc/ppc.h"
  36. #include "e500.h"
  37. #include "qom/object.h"
  38. #define MAX_CPUS 32
  39. typedef struct spin_info {
  40. uint64_t addr;
  41. uint64_t r3;
  42. uint32_t resv;
  43. uint32_t pir;
  44. uint64_t reserved;
  45. } QEMU_PACKED SpinInfo;
  46. #define TYPE_E500_SPIN "e500-spin"
  47. OBJECT_DECLARE_SIMPLE_TYPE(SpinState, E500_SPIN)
  48. struct SpinState {
  49. SysBusDevice parent_obj;
  50. MemoryRegion iomem;
  51. SpinInfo spin[MAX_CPUS];
  52. };
  53. static void spin_reset(DeviceState *dev)
  54. {
  55. SpinState *s = E500_SPIN(dev);
  56. int i;
  57. for (i = 0; i < MAX_CPUS; i++) {
  58. SpinInfo *info = &s->spin[i];
  59. stl_p(&info->pir, i);
  60. stq_p(&info->r3, i);
  61. stq_p(&info->addr, 1);
  62. }
  63. }
  64. static void spin_kick(CPUState *cs, run_on_cpu_data data)
  65. {
  66. CPUPPCState *env = cpu_env(cs);
  67. SpinInfo *curspin = data.host_ptr;
  68. hwaddr map_start, map_size = 64 * MiB;
  69. ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 1);
  70. cpu_synchronize_state(cs);
  71. stl_p(&curspin->pir, env->spr[SPR_BOOKE_PIR]);
  72. env->nip = ldq_p(&curspin->addr) & (map_size - 1);
  73. env->gpr[3] = ldq_p(&curspin->r3);
  74. env->gpr[4] = 0;
  75. env->gpr[5] = 0;
  76. env->gpr[6] = 0;
  77. env->gpr[7] = map_size;
  78. env->gpr[8] = 0;
  79. env->gpr[9] = 0;
  80. map_start = ldq_p(&curspin->addr) & ~(map_size - 1);
  81. /* create initial mapping */
  82. booke206_set_tlb(tlb, 0, map_start, map_size);
  83. tlb->mas2 |= MAS2_M;
  84. #ifdef CONFIG_KVM
  85. env->tlb_dirty = true;
  86. #endif
  87. cs->halted = 0;
  88. cs->exception_index = -1;
  89. cs->stopped = false;
  90. qemu_cpu_kick(cs);
  91. }
  92. static void spin_write(void *opaque, hwaddr addr, uint64_t value,
  93. unsigned len)
  94. {
  95. SpinState *s = opaque;
  96. int env_idx = addr / sizeof(SpinInfo);
  97. CPUState *cpu;
  98. SpinInfo *curspin = &s->spin[env_idx];
  99. uint8_t *curspin_p = (uint8_t*)curspin;
  100. cpu = qemu_get_cpu(env_idx);
  101. if (cpu == NULL) {
  102. /* Unknown CPU */
  103. return;
  104. }
  105. if (cpu->cpu_index == 0) {
  106. /* primary CPU doesn't spin */
  107. return;
  108. }
  109. curspin_p = &curspin_p[addr % sizeof(SpinInfo)];
  110. switch (len) {
  111. case 1:
  112. stb_p(curspin_p, value);
  113. break;
  114. case 2:
  115. stw_p(curspin_p, value);
  116. break;
  117. case 4:
  118. stl_p(curspin_p, value);
  119. break;
  120. }
  121. if (!(ldq_p(&curspin->addr) & 1)) {
  122. /* run CPU */
  123. run_on_cpu(cpu, spin_kick, RUN_ON_CPU_HOST_PTR(curspin));
  124. }
  125. }
  126. static uint64_t spin_read(void *opaque, hwaddr addr, unsigned len)
  127. {
  128. SpinState *s = opaque;
  129. uint8_t *spin_p = &((uint8_t*)s->spin)[addr];
  130. switch (len) {
  131. case 1:
  132. return ldub_p(spin_p);
  133. case 2:
  134. return lduw_p(spin_p);
  135. case 4:
  136. return ldl_p(spin_p);
  137. default:
  138. hw_error("ppce500: unexpected %s with len = %u", __func__, len);
  139. }
  140. }
  141. static const MemoryRegionOps spin_rw_ops = {
  142. .read = spin_read,
  143. .write = spin_write,
  144. .endianness = DEVICE_BIG_ENDIAN,
  145. };
  146. static void ppce500_spin_initfn(Object *obj)
  147. {
  148. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  149. SpinState *s = E500_SPIN(dev);
  150. memory_region_init_io(&s->iomem, obj, &spin_rw_ops, s,
  151. "e500 spin pv device", sizeof(SpinInfo) * MAX_CPUS);
  152. sysbus_init_mmio(dev, &s->iomem);
  153. }
  154. static void ppce500_spin_class_init(ObjectClass *klass, void *data)
  155. {
  156. DeviceClass *dc = DEVICE_CLASS(klass);
  157. device_class_set_legacy_reset(dc, spin_reset);
  158. }
  159. static const TypeInfo ppce500_spin_info = {
  160. .name = TYPE_E500_SPIN,
  161. .parent = TYPE_SYS_BUS_DEVICE,
  162. .instance_size = sizeof(SpinState),
  163. .instance_init = ppce500_spin_initfn,
  164. .class_init = ppce500_spin_class_init,
  165. };
  166. static void ppce500_spin_register_types(void)
  167. {
  168. type_register_static(&ppce500_spin_info);
  169. }
  170. type_init(ppce500_spin_register_types)