riscv_hart.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * QEMU RISCV Hart Array
  3. *
  4. * Copyright (c) 2017 SiFive, Inc.
  5. *
  6. * Holds the state of a homogeneous array of RISC-V harts
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2 or later, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/module.h"
  23. #include "sysemu/reset.h"
  24. #include "hw/sysbus.h"
  25. #include "target/riscv/cpu.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/riscv/riscv_hart.h"
  28. static Property riscv_harts_props[] = {
  29. DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts, 1),
  30. DEFINE_PROP_UINT32("hartid-base", RISCVHartArrayState, hartid_base, 0),
  31. DEFINE_PROP_STRING("cpu-type", RISCVHartArrayState, cpu_type),
  32. DEFINE_PROP_END_OF_LIST(),
  33. };
  34. static void riscv_harts_cpu_reset(void *opaque)
  35. {
  36. RISCVCPU *cpu = opaque;
  37. cpu_reset(CPU(cpu));
  38. }
  39. static void riscv_hart_realize(RISCVHartArrayState *s, int idx,
  40. char *cpu_type, Error **errp)
  41. {
  42. Error *err = NULL;
  43. object_initialize_child(OBJECT(s), "harts[*]", &s->harts[idx],
  44. sizeof(RISCVCPU), cpu_type,
  45. &error_abort, NULL);
  46. s->harts[idx].env.mhartid = s->hartid_base + idx;
  47. qemu_register_reset(riscv_harts_cpu_reset, &s->harts[idx]);
  48. object_property_set_bool(OBJECT(&s->harts[idx]), true,
  49. "realized", &err);
  50. if (err) {
  51. error_propagate(errp, err);
  52. return;
  53. }
  54. }
  55. static void riscv_harts_realize(DeviceState *dev, Error **errp)
  56. {
  57. RISCVHartArrayState *s = RISCV_HART_ARRAY(dev);
  58. int n;
  59. s->harts = g_new0(RISCVCPU, s->num_harts);
  60. for (n = 0; n < s->num_harts; n++) {
  61. riscv_hart_realize(s, n, s->cpu_type, errp);
  62. }
  63. }
  64. static void riscv_harts_class_init(ObjectClass *klass, void *data)
  65. {
  66. DeviceClass *dc = DEVICE_CLASS(klass);
  67. dc->props = riscv_harts_props;
  68. dc->realize = riscv_harts_realize;
  69. }
  70. static const TypeInfo riscv_harts_info = {
  71. .name = TYPE_RISCV_HART_ARRAY,
  72. .parent = TYPE_SYS_BUS_DEVICE,
  73. .instance_size = sizeof(RISCVHartArrayState),
  74. .class_init = riscv_harts_class_init,
  75. };
  76. static void riscv_harts_register_types(void)
  77. {
  78. type_register_static(&riscv_harts_info);
  79. }
  80. type_init(riscv_harts_register_types)