2
0

cluster.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * QEMU CPU cluster
  3. *
  4. * Copyright (c) 2018 GreenSocs SAS
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (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
  18. * <http://www.gnu.org/licenses/gpl-2.0.html>
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/cpu/cluster.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/core/cpu.h"
  24. #include "qapi/error.h"
  25. #include "qemu/module.h"
  26. #include "qemu/cutils.h"
  27. static Property cpu_cluster_properties[] = {
  28. DEFINE_PROP_UINT32("cluster-id", CPUClusterState, cluster_id, 0),
  29. DEFINE_PROP_END_OF_LIST()
  30. };
  31. typedef struct CallbackData {
  32. CPUClusterState *cluster;
  33. int cpu_count;
  34. } CallbackData;
  35. static int add_cpu_to_cluster(Object *obj, void *opaque)
  36. {
  37. CallbackData *cbdata = opaque;
  38. CPUState *cpu = (CPUState *)object_dynamic_cast(obj, TYPE_CPU);
  39. if (cpu) {
  40. cpu->cluster_index = cbdata->cluster->cluster_id;
  41. cbdata->cpu_count++;
  42. }
  43. return 0;
  44. }
  45. static void cpu_cluster_realize(DeviceState *dev, Error **errp)
  46. {
  47. /* Iterate through all our CPU children and set their cluster_index */
  48. CPUClusterState *cluster = CPU_CLUSTER(dev);
  49. Object *cluster_obj = OBJECT(dev);
  50. CallbackData cbdata = {
  51. .cluster = cluster,
  52. .cpu_count = 0,
  53. };
  54. if (cluster->cluster_id >= MAX_CLUSTERS) {
  55. error_setg(errp, "cluster-id must be less than %d", MAX_CLUSTERS);
  56. return;
  57. }
  58. object_child_foreach_recursive(cluster_obj, add_cpu_to_cluster, &cbdata);
  59. /*
  60. * A cluster with no CPUs is a bug in the board/SoC code that created it;
  61. * if you hit this during development of new code, check that you have
  62. * created the CPUs and parented them into the cluster object before
  63. * realizing the cluster object.
  64. */
  65. assert(cbdata.cpu_count > 0);
  66. }
  67. static void cpu_cluster_class_init(ObjectClass *klass, void *data)
  68. {
  69. DeviceClass *dc = DEVICE_CLASS(klass);
  70. dc->props = cpu_cluster_properties;
  71. dc->realize = cpu_cluster_realize;
  72. /* This is not directly for users, CPU children must be attached by code */
  73. dc->user_creatable = false;
  74. }
  75. static const TypeInfo cpu_cluster_type_info = {
  76. .name = TYPE_CPU_CLUSTER,
  77. .parent = TYPE_DEVICE,
  78. .instance_size = sizeof(CPUClusterState),
  79. .class_init = cpu_cluster_class_init,
  80. };
  81. static void cpu_cluster_register_types(void)
  82. {
  83. type_register_static(&cpu_cluster_type_info);
  84. }
  85. type_init(cpu_cluster_register_types)