2 * arch/arm64/kernel/topology.c
4 * Copyright (C) 2011,2013,2014 Linaro Limited.
6 * Based on the arm32 version written by Vincent Guittot in turn based on
7 * arch/sh/kernel/topology.c
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/cpu.h>
15 #include <linux/cpumask.h>
16 #include <linux/init.h>
17 #include <linux/percpu.h>
18 #include <linux/node.h>
19 #include <linux/nodemask.h>
21 #include <linux/sched.h>
23 #include <asm/cputype.h>
24 #include <asm/topology.h>
26 static int __init
get_cpu_for_node(struct device_node
*node
)
28 struct device_node
*cpu_node
;
31 cpu_node
= of_parse_phandle(node
, "cpu", 0);
35 for_each_possible_cpu(cpu
) {
36 if (of_get_cpu_node(cpu
, NULL
) == cpu_node
) {
37 of_node_put(cpu_node
);
42 pr_crit("Unable to find CPU node for %s\n", cpu_node
->full_name
);
44 of_node_put(cpu_node
);
48 static int __init
parse_core(struct device_node
*core
, int cluster_id
,
55 struct device_node
*t
;
58 snprintf(name
, sizeof(name
), "thread%d", i
);
59 t
= of_get_child_by_name(core
, name
);
62 cpu
= get_cpu_for_node(t
);
64 cpu_topology
[cpu
].cluster_id
= cluster_id
;
65 cpu_topology
[cpu
].core_id
= core_id
;
66 cpu_topology
[cpu
].thread_id
= i
;
68 pr_err("%s: Can't get CPU for thread\n",
78 cpu
= get_cpu_for_node(core
);
81 pr_err("%s: Core has both threads and CPU\n",
86 cpu_topology
[cpu
].cluster_id
= cluster_id
;
87 cpu_topology
[cpu
].core_id
= core_id
;
89 pr_err("%s: Can't get CPU for leaf core\n", core
->full_name
);
96 static int __init
parse_cluster(struct device_node
*cluster
, int depth
)
100 bool has_cores
= false;
101 struct device_node
*c
;
102 static int cluster_id __initdata
;
107 * First check for child clusters; we currently ignore any
108 * information about the nesting of clusters and present the
109 * scheduler with a flat list of them.
113 snprintf(name
, sizeof(name
), "cluster%d", i
);
114 c
= of_get_child_by_name(cluster
, name
);
117 ret
= parse_cluster(c
, depth
+ 1);
125 /* Now check for cores */
128 snprintf(name
, sizeof(name
), "core%d", i
);
129 c
= of_get_child_by_name(cluster
, name
);
134 pr_err("%s: cpu-map children should be clusters\n",
141 ret
= parse_core(c
, cluster_id
, core_id
++);
143 pr_err("%s: Non-leaf cluster with core %s\n",
144 cluster
->full_name
, name
);
155 if (leaf
&& !has_cores
)
156 pr_warn("%s: empty cluster\n", cluster
->full_name
);
164 static int __init
parse_dt_topology(void)
166 struct device_node
*cn
, *map
;
170 cn
= of_find_node_by_path("/cpus");
172 pr_err("No CPU information found in DT\n");
177 * When topology is provided cpu-map is essentially a root
178 * cluster with restricted subnodes.
180 map
= of_get_child_by_name(cn
, "cpu-map");
184 ret
= parse_cluster(map
, 0);
189 * Check that all cores are in the topology; the SMP code will
190 * only mark cores described in the DT as possible.
192 for_each_possible_cpu(cpu
)
193 if (cpu_topology
[cpu
].cluster_id
== -1)
206 struct cpu_topology cpu_topology
[NR_CPUS
];
207 EXPORT_SYMBOL_GPL(cpu_topology
);
209 const struct cpumask
*cpu_coregroup_mask(int cpu
)
211 return &cpu_topology
[cpu
].core_sibling
;
214 static void update_siblings_masks(unsigned int cpuid
)
216 struct cpu_topology
*cpu_topo
, *cpuid_topo
= &cpu_topology
[cpuid
];
219 /* update core and thread sibling masks */
220 for_each_possible_cpu(cpu
) {
221 cpu_topo
= &cpu_topology
[cpu
];
223 if (cpuid_topo
->cluster_id
!= cpu_topo
->cluster_id
)
226 cpumask_set_cpu(cpuid
, &cpu_topo
->core_sibling
);
228 cpumask_set_cpu(cpu
, &cpuid_topo
->core_sibling
);
230 if (cpuid_topo
->core_id
!= cpu_topo
->core_id
)
233 cpumask_set_cpu(cpuid
, &cpu_topo
->thread_sibling
);
235 cpumask_set_cpu(cpu
, &cpuid_topo
->thread_sibling
);
239 void store_cpu_topology(unsigned int cpuid
)
241 struct cpu_topology
*cpuid_topo
= &cpu_topology
[cpuid
];
244 if (cpuid_topo
->cluster_id
!= -1)
245 goto topology_populated
;
247 mpidr
= read_cpuid_mpidr();
249 /* Uniprocessor systems can rely on default topology values */
250 if (mpidr
& MPIDR_UP_BITMASK
)
253 /* Create cpu topology mapping based on MPIDR. */
254 if (mpidr
& MPIDR_MT_BITMASK
) {
255 /* Multiprocessor system : Multi-threads per core */
256 cpuid_topo
->thread_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
257 cpuid_topo
->core_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 1);
258 cpuid_topo
->cluster_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 2) |
259 MPIDR_AFFINITY_LEVEL(mpidr
, 3) << 8;
261 /* Multiprocessor system : Single-thread per core */
262 cpuid_topo
->thread_id
= -1;
263 cpuid_topo
->core_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 0);
264 cpuid_topo
->cluster_id
= MPIDR_AFFINITY_LEVEL(mpidr
, 1) |
265 MPIDR_AFFINITY_LEVEL(mpidr
, 2) << 8 |
266 MPIDR_AFFINITY_LEVEL(mpidr
, 3) << 16;
269 pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
270 cpuid
, cpuid_topo
->cluster_id
, cpuid_topo
->core_id
,
271 cpuid_topo
->thread_id
, mpidr
);
274 update_siblings_masks(cpuid
);
277 static void __init
reset_cpu_topology(void)
281 for_each_possible_cpu(cpu
) {
282 struct cpu_topology
*cpu_topo
= &cpu_topology
[cpu
];
284 cpu_topo
->thread_id
= -1;
285 cpu_topo
->core_id
= 0;
286 cpu_topo
->cluster_id
= -1;
288 cpumask_clear(&cpu_topo
->core_sibling
);
289 cpumask_set_cpu(cpu
, &cpu_topo
->core_sibling
);
290 cpumask_clear(&cpu_topo
->thread_sibling
);
291 cpumask_set_cpu(cpu
, &cpu_topo
->thread_sibling
);
295 void __init
init_cpu_topology(void)
297 reset_cpu_topology();
300 * Discard anything that was parsed if we hit an error so we
301 * don't use partial information.
303 if (of_have_populated_dt() && parse_dt_topology())
304 reset_cpu_topology();