4 * Copyright (c) 2018 GreenSocs SAS
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.
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.
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>
21 #include "qemu/osdep.h"
23 #include "hw/core/cpu.h"
24 #include "hw/cpu/cluster.h"
25 #include "hw/qdev-properties.h"
26 #include "qapi/error.h"
28 static Property cpu_cluster_properties
[] = {
29 DEFINE_PROP_UINT32("cluster-id", CPUClusterState
, cluster_id
, 0),
30 DEFINE_PROP_END_OF_LIST()
33 typedef struct CallbackData
{
34 CPUClusterState
*cluster
;
38 static int add_cpu_to_cluster(Object
*obj
, void *opaque
)
40 CallbackData
*cbdata
= opaque
;
41 CPUState
*cpu
= (CPUState
*)object_dynamic_cast(obj
, TYPE_CPU
);
44 cpu
->cluster_index
= cbdata
->cluster
->cluster_id
;
50 static void cpu_cluster_realize(DeviceState
*dev
, Error
**errp
)
52 /* Iterate through all our CPU children and set their cluster_index */
53 CPUClusterState
*cluster
= CPU_CLUSTER(dev
);
54 Object
*cluster_obj
= OBJECT(dev
);
55 CallbackData cbdata
= {
60 if (cluster
->cluster_id
>= MAX_CLUSTERS
) {
61 error_setg(errp
, "cluster-id must be less than %d", MAX_CLUSTERS
);
65 object_child_foreach_recursive(cluster_obj
, add_cpu_to_cluster
, &cbdata
);
68 * A cluster with no CPUs is a bug in the board/SoC code that created it;
69 * if you hit this during development of new code, check that you have
70 * created the CPUs and parented them into the cluster object before
71 * realizing the cluster object.
73 assert(cbdata
.cpu_count
> 0);
76 static void cpu_cluster_class_init(ObjectClass
*klass
, void *data
)
78 DeviceClass
*dc
= DEVICE_CLASS(klass
);
80 device_class_set_props(dc
, cpu_cluster_properties
);
81 dc
->realize
= cpu_cluster_realize
;
83 /* This is not directly for users, CPU children must be attached by code */
84 dc
->user_creatable
= false;
87 static const TypeInfo cpu_cluster_type_info
= {
88 .name
= TYPE_CPU_CLUSTER
,
89 .parent
= TYPE_DEVICE
,
90 .instance_size
= sizeof(CPUClusterState
),
91 .class_init
= cpu_cluster_class_init
,
94 static void cpu_cluster_register_types(void)
96 type_register_static(&cpu_cluster_type_info
);
99 type_init(cpu_cluster_register_types
)