1335d52f4SLuc Michel /*
2335d52f4SLuc Michel * QEMU CPU cluster
3335d52f4SLuc Michel *
4335d52f4SLuc Michel * Copyright (c) 2018 GreenSocs SAS
5335d52f4SLuc Michel *
6335d52f4SLuc Michel * This program is free software; you can redistribute it and/or
7335d52f4SLuc Michel * modify it under the terms of the GNU General Public License
8335d52f4SLuc Michel * as published by the Free Software Foundation; either version 2
9335d52f4SLuc Michel * of the License, or (at your option) any later version.
10335d52f4SLuc Michel *
11335d52f4SLuc Michel * This program is distributed in the hope that it will be useful,
12335d52f4SLuc Michel * but WITHOUT ANY WARRANTY; without even the implied warranty of
13335d52f4SLuc Michel * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14335d52f4SLuc Michel * GNU General Public License for more details.
15335d52f4SLuc Michel *
16335d52f4SLuc Michel * You should have received a copy of the GNU General Public License
17335d52f4SLuc Michel * along with this program; if not, see
18335d52f4SLuc Michel * <http://www.gnu.org/licenses/gpl-2.0.html>
19335d52f4SLuc Michel */
20335d52f4SLuc Michel
21335d52f4SLuc Michel #include "qemu/osdep.h"
22*a8a9f698SZhao Liu
23*a8a9f698SZhao Liu #include "hw/core/cpu.h"
24335d52f4SLuc Michel #include "hw/cpu/cluster.h"
25a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
26335d52f4SLuc Michel #include "qapi/error.h"
27335d52f4SLuc Michel
28335d52f4SLuc Michel static Property cpu_cluster_properties[] = {
29335d52f4SLuc Michel DEFINE_PROP_UINT32("cluster-id", CPUClusterState, cluster_id, 0),
30335d52f4SLuc Michel DEFINE_PROP_END_OF_LIST()
31335d52f4SLuc Michel };
32335d52f4SLuc Michel
337ea7b9adSPeter Maydell typedef struct CallbackData {
347ea7b9adSPeter Maydell CPUClusterState *cluster;
357ea7b9adSPeter Maydell int cpu_count;
367ea7b9adSPeter Maydell } CallbackData;
377ea7b9adSPeter Maydell
add_cpu_to_cluster(Object * obj,void * opaque)387ea7b9adSPeter Maydell static int add_cpu_to_cluster(Object *obj, void *opaque)
397ea7b9adSPeter Maydell {
407ea7b9adSPeter Maydell CallbackData *cbdata = opaque;
417ea7b9adSPeter Maydell CPUState *cpu = (CPUState *)object_dynamic_cast(obj, TYPE_CPU);
427ea7b9adSPeter Maydell
437ea7b9adSPeter Maydell if (cpu) {
447ea7b9adSPeter Maydell cpu->cluster_index = cbdata->cluster->cluster_id;
457ea7b9adSPeter Maydell cbdata->cpu_count++;
467ea7b9adSPeter Maydell }
477ea7b9adSPeter Maydell return 0;
487ea7b9adSPeter Maydell }
497ea7b9adSPeter Maydell
cpu_cluster_realize(DeviceState * dev,Error ** errp)507ea7b9adSPeter Maydell static void cpu_cluster_realize(DeviceState *dev, Error **errp)
517ea7b9adSPeter Maydell {
527ea7b9adSPeter Maydell /* Iterate through all our CPU children and set their cluster_index */
537ea7b9adSPeter Maydell CPUClusterState *cluster = CPU_CLUSTER(dev);
547ea7b9adSPeter Maydell Object *cluster_obj = OBJECT(dev);
557ea7b9adSPeter Maydell CallbackData cbdata = {
567ea7b9adSPeter Maydell .cluster = cluster,
577ea7b9adSPeter Maydell .cpu_count = 0,
587ea7b9adSPeter Maydell };
597ea7b9adSPeter Maydell
607ea7b9adSPeter Maydell if (cluster->cluster_id >= MAX_CLUSTERS) {
617ea7b9adSPeter Maydell error_setg(errp, "cluster-id must be less than %d", MAX_CLUSTERS);
627ea7b9adSPeter Maydell return;
637ea7b9adSPeter Maydell }
647ea7b9adSPeter Maydell
657ea7b9adSPeter Maydell object_child_foreach_recursive(cluster_obj, add_cpu_to_cluster, &cbdata);
667ea7b9adSPeter Maydell
677ea7b9adSPeter Maydell /*
687ea7b9adSPeter Maydell * A cluster with no CPUs is a bug in the board/SoC code that created it;
697ea7b9adSPeter Maydell * if you hit this during development of new code, check that you have
707ea7b9adSPeter Maydell * created the CPUs and parented them into the cluster object before
717ea7b9adSPeter Maydell * realizing the cluster object.
727ea7b9adSPeter Maydell */
737ea7b9adSPeter Maydell assert(cbdata.cpu_count > 0);
747ea7b9adSPeter Maydell }
757ea7b9adSPeter Maydell
cpu_cluster_class_init(ObjectClass * klass,void * data)76335d52f4SLuc Michel static void cpu_cluster_class_init(ObjectClass *klass, void *data)
77335d52f4SLuc Michel {
78335d52f4SLuc Michel DeviceClass *dc = DEVICE_CLASS(klass);
79335d52f4SLuc Michel
804f67d30bSMarc-André Lureau device_class_set_props(dc, cpu_cluster_properties);
817ea7b9adSPeter Maydell dc->realize = cpu_cluster_realize;
82dab864dcSThomas Huth
83dab864dcSThomas Huth /* This is not directly for users, CPU children must be attached by code */
84dab864dcSThomas Huth dc->user_creatable = false;
85335d52f4SLuc Michel }
86335d52f4SLuc Michel
87335d52f4SLuc Michel static const TypeInfo cpu_cluster_type_info = {
88335d52f4SLuc Michel .name = TYPE_CPU_CLUSTER,
89335d52f4SLuc Michel .parent = TYPE_DEVICE,
90335d52f4SLuc Michel .instance_size = sizeof(CPUClusterState),
91335d52f4SLuc Michel .class_init = cpu_cluster_class_init,
92335d52f4SLuc Michel };
93335d52f4SLuc Michel
cpu_cluster_register_types(void)94335d52f4SLuc Michel static void cpu_cluster_register_types(void)
95335d52f4SLuc Michel {
96335d52f4SLuc Michel type_register_static(&cpu_cluster_type_info);
97335d52f4SLuc Michel }
98335d52f4SLuc Michel
99335d52f4SLuc Michel type_init(cpu_cluster_register_types)
100