1 // SPDX-License-Identifier: GPL-2.0
3 * Arch specific cpu topology information
5 * Copyright (C) 2016, ARM Ltd.
6 * Written by: Juri Lelli, ARM Ltd.
9 #include <linux/acpi.h>
10 #include <linux/arch_topology.h>
11 #include <linux/cpu.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/sched/topology.h>
18 #include <linux/cpuset.h>
20 DEFINE_PER_CPU(unsigned long, freq_scale
) = SCHED_CAPACITY_SCALE
;
22 void arch_set_freq_scale(struct cpumask
*cpus
, unsigned long cur_freq
,
23 unsigned long max_freq
)
28 scale
= (cur_freq
<< SCHED_CAPACITY_SHIFT
) / max_freq
;
31 per_cpu(freq_scale
, i
) = scale
;
34 static DEFINE_MUTEX(cpu_scale_mutex
);
35 DEFINE_PER_CPU(unsigned long, cpu_scale
) = SCHED_CAPACITY_SCALE
;
37 void topology_set_cpu_scale(unsigned int cpu
, unsigned long capacity
)
39 per_cpu(cpu_scale
, cpu
) = capacity
;
42 static ssize_t
cpu_capacity_show(struct device
*dev
,
43 struct device_attribute
*attr
,
46 struct cpu
*cpu
= container_of(dev
, struct cpu
, dev
);
48 return sprintf(buf
, "%lu\n", topology_get_cpu_scale(NULL
, cpu
->dev
.id
));
51 static void update_topology_flags_workfn(struct work_struct
*work
);
52 static DECLARE_WORK(update_topology_flags_work
, update_topology_flags_workfn
);
54 static ssize_t
cpu_capacity_store(struct device
*dev
,
55 struct device_attribute
*attr
,
59 struct cpu
*cpu
= container_of(dev
, struct cpu
, dev
);
60 int this_cpu
= cpu
->dev
.id
;
62 unsigned long new_capacity
;
68 ret
= kstrtoul(buf
, 0, &new_capacity
);
71 if (new_capacity
> SCHED_CAPACITY_SCALE
)
74 mutex_lock(&cpu_scale_mutex
);
75 for_each_cpu(i
, &cpu_topology
[this_cpu
].core_sibling
)
76 topology_set_cpu_scale(i
, new_capacity
);
77 mutex_unlock(&cpu_scale_mutex
);
79 schedule_work(&update_topology_flags_work
);
84 static DEVICE_ATTR_RW(cpu_capacity
);
86 static int register_cpu_capacity_sysctl(void)
91 for_each_possible_cpu(i
) {
92 cpu
= get_cpu_device(i
);
94 pr_err("%s: too early to get CPU%d device!\n",
98 device_create_file(cpu
, &dev_attr_cpu_capacity
);
103 subsys_initcall(register_cpu_capacity_sysctl
);
105 static int update_topology
;
107 int topology_update_cpu_topology(void)
109 return update_topology
;
113 * Updating the sched_domains can't be done directly from cpufreq callbacks
114 * due to locking, so queue the work for later.
116 static void update_topology_flags_workfn(struct work_struct
*work
)
119 rebuild_sched_domains();
120 pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
124 static u32 capacity_scale
;
125 static u32
*raw_capacity
;
127 static int free_raw_capacity(void)
135 void topology_normalize_cpu_scale(void)
143 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale
);
144 mutex_lock(&cpu_scale_mutex
);
145 for_each_possible_cpu(cpu
) {
146 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
147 cpu
, raw_capacity
[cpu
]);
148 capacity
= (raw_capacity
[cpu
] << SCHED_CAPACITY_SHIFT
)
150 topology_set_cpu_scale(cpu
, capacity
);
151 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
152 cpu
, topology_get_cpu_scale(NULL
, cpu
));
154 mutex_unlock(&cpu_scale_mutex
);
157 bool __init
topology_parse_cpu_capacity(struct device_node
*cpu_node
, int cpu
)
159 static bool cap_parsing_failed
;
163 if (cap_parsing_failed
)
166 ret
= of_property_read_u32(cpu_node
, "capacity-dmips-mhz",
170 raw_capacity
= kcalloc(num_possible_cpus(),
171 sizeof(*raw_capacity
),
174 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
175 cap_parsing_failed
= true;
179 capacity_scale
= max(cpu_capacity
, capacity_scale
);
180 raw_capacity
[cpu
] = cpu_capacity
;
181 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
182 cpu_node
, raw_capacity
[cpu
]);
185 pr_err("cpu_capacity: missing %pOF raw capacity\n",
187 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
189 cap_parsing_failed
= true;
196 #ifdef CONFIG_CPU_FREQ
197 static cpumask_var_t cpus_to_visit
;
198 static void parsing_done_workfn(struct work_struct
*work
);
199 static DECLARE_WORK(parsing_done_work
, parsing_done_workfn
);
202 init_cpu_capacity_callback(struct notifier_block
*nb
,
206 struct cpufreq_policy
*policy
= data
;
212 if (val
!= CPUFREQ_NOTIFY
)
215 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
216 cpumask_pr_args(policy
->related_cpus
),
217 cpumask_pr_args(cpus_to_visit
));
219 cpumask_andnot(cpus_to_visit
, cpus_to_visit
, policy
->related_cpus
);
221 for_each_cpu(cpu
, policy
->related_cpus
) {
222 raw_capacity
[cpu
] = topology_get_cpu_scale(NULL
, cpu
) *
223 policy
->cpuinfo
.max_freq
/ 1000UL;
224 capacity_scale
= max(raw_capacity
[cpu
], capacity_scale
);
227 if (cpumask_empty(cpus_to_visit
)) {
228 topology_normalize_cpu_scale();
229 schedule_work(&update_topology_flags_work
);
231 pr_debug("cpu_capacity: parsing done\n");
232 schedule_work(&parsing_done_work
);
238 static struct notifier_block init_cpu_capacity_notifier
= {
239 .notifier_call
= init_cpu_capacity_callback
,
242 static int __init
register_cpufreq_notifier(void)
247 * on ACPI-based systems we need to use the default cpu capacity
248 * until we have the necessary code to parse the cpu capacity, so
249 * skip registering cpufreq notifier.
251 if (!acpi_disabled
|| !raw_capacity
)
254 if (!alloc_cpumask_var(&cpus_to_visit
, GFP_KERNEL
)) {
255 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
259 cpumask_copy(cpus_to_visit
, cpu_possible_mask
);
261 ret
= cpufreq_register_notifier(&init_cpu_capacity_notifier
,
262 CPUFREQ_POLICY_NOTIFIER
);
265 free_cpumask_var(cpus_to_visit
);
269 core_initcall(register_cpufreq_notifier
);
271 static void parsing_done_workfn(struct work_struct
*work
)
273 cpufreq_unregister_notifier(&init_cpu_capacity_notifier
,
274 CPUFREQ_POLICY_NOTIFIER
);
275 free_cpumask_var(cpus_to_visit
);
279 core_initcall(free_raw_capacity
);