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>
19 DEFINE_PER_CPU(unsigned long, freq_scale
) = SCHED_CAPACITY_SCALE
;
21 void arch_set_freq_scale(struct cpumask
*cpus
, unsigned long cur_freq
,
22 unsigned long max_freq
)
27 scale
= (cur_freq
<< SCHED_CAPACITY_SHIFT
) / max_freq
;
30 per_cpu(freq_scale
, i
) = scale
;
33 static DEFINE_MUTEX(cpu_scale_mutex
);
34 DEFINE_PER_CPU(unsigned long, cpu_scale
) = SCHED_CAPACITY_SCALE
;
36 void topology_set_cpu_scale(unsigned int cpu
, unsigned long capacity
)
38 per_cpu(cpu_scale
, cpu
) = capacity
;
41 static ssize_t
cpu_capacity_show(struct device
*dev
,
42 struct device_attribute
*attr
,
45 struct cpu
*cpu
= container_of(dev
, struct cpu
, dev
);
47 return sprintf(buf
, "%lu\n", topology_get_cpu_scale(NULL
, cpu
->dev
.id
));
50 static ssize_t
cpu_capacity_store(struct device
*dev
,
51 struct device_attribute
*attr
,
55 struct cpu
*cpu
= container_of(dev
, struct cpu
, dev
);
56 int this_cpu
= cpu
->dev
.id
;
58 unsigned long new_capacity
;
64 ret
= kstrtoul(buf
, 0, &new_capacity
);
67 if (new_capacity
> SCHED_CAPACITY_SCALE
)
70 mutex_lock(&cpu_scale_mutex
);
71 for_each_cpu(i
, &cpu_topology
[this_cpu
].core_sibling
)
72 topology_set_cpu_scale(i
, new_capacity
);
73 mutex_unlock(&cpu_scale_mutex
);
78 static DEVICE_ATTR_RW(cpu_capacity
);
80 static int register_cpu_capacity_sysctl(void)
85 for_each_possible_cpu(i
) {
86 cpu
= get_cpu_device(i
);
88 pr_err("%s: too early to get CPU%d device!\n",
92 device_create_file(cpu
, &dev_attr_cpu_capacity
);
97 subsys_initcall(register_cpu_capacity_sysctl
);
99 static u32 capacity_scale
;
100 static u32
*raw_capacity
;
102 static int free_raw_capacity(void)
110 void topology_normalize_cpu_scale(void)
118 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale
);
119 mutex_lock(&cpu_scale_mutex
);
120 for_each_possible_cpu(cpu
) {
121 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
122 cpu
, raw_capacity
[cpu
]);
123 capacity
= (raw_capacity
[cpu
] << SCHED_CAPACITY_SHIFT
)
125 topology_set_cpu_scale(cpu
, capacity
);
126 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
127 cpu
, topology_get_cpu_scale(NULL
, cpu
));
129 mutex_unlock(&cpu_scale_mutex
);
132 bool __init
topology_parse_cpu_capacity(struct device_node
*cpu_node
, int cpu
)
134 static bool cap_parsing_failed
;
138 if (cap_parsing_failed
)
141 ret
= of_property_read_u32(cpu_node
, "capacity-dmips-mhz",
145 raw_capacity
= kcalloc(num_possible_cpus(),
146 sizeof(*raw_capacity
),
149 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
150 cap_parsing_failed
= true;
154 capacity_scale
= max(cpu_capacity
, capacity_scale
);
155 raw_capacity
[cpu
] = cpu_capacity
;
156 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
157 cpu_node
, raw_capacity
[cpu
]);
160 pr_err("cpu_capacity: missing %pOF raw capacity\n",
162 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
164 cap_parsing_failed
= true;
171 #ifdef CONFIG_CPU_FREQ
172 static cpumask_var_t cpus_to_visit
;
173 static void parsing_done_workfn(struct work_struct
*work
);
174 static DECLARE_WORK(parsing_done_work
, parsing_done_workfn
);
177 init_cpu_capacity_callback(struct notifier_block
*nb
,
181 struct cpufreq_policy
*policy
= data
;
187 if (val
!= CPUFREQ_NOTIFY
)
190 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
191 cpumask_pr_args(policy
->related_cpus
),
192 cpumask_pr_args(cpus_to_visit
));
194 cpumask_andnot(cpus_to_visit
, cpus_to_visit
, policy
->related_cpus
);
196 for_each_cpu(cpu
, policy
->related_cpus
) {
197 raw_capacity
[cpu
] = topology_get_cpu_scale(NULL
, cpu
) *
198 policy
->cpuinfo
.max_freq
/ 1000UL;
199 capacity_scale
= max(raw_capacity
[cpu
], capacity_scale
);
202 if (cpumask_empty(cpus_to_visit
)) {
203 topology_normalize_cpu_scale();
205 pr_debug("cpu_capacity: parsing done\n");
206 schedule_work(&parsing_done_work
);
212 static struct notifier_block init_cpu_capacity_notifier
= {
213 .notifier_call
= init_cpu_capacity_callback
,
216 static int __init
register_cpufreq_notifier(void)
221 * on ACPI-based systems we need to use the default cpu capacity
222 * until we have the necessary code to parse the cpu capacity, so
223 * skip registering cpufreq notifier.
225 if (!acpi_disabled
|| !raw_capacity
)
228 if (!alloc_cpumask_var(&cpus_to_visit
, GFP_KERNEL
)) {
229 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
233 cpumask_copy(cpus_to_visit
, cpu_possible_mask
);
235 ret
= cpufreq_register_notifier(&init_cpu_capacity_notifier
,
236 CPUFREQ_POLICY_NOTIFIER
);
239 free_cpumask_var(cpus_to_visit
);
243 core_initcall(register_cpufreq_notifier
);
245 static void parsing_done_workfn(struct work_struct
*work
)
247 cpufreq_unregister_notifier(&init_cpu_capacity_notifier
,
248 CPUFREQ_POLICY_NOTIFIER
);
249 free_cpumask_var(cpus_to_visit
);
253 core_initcall(free_raw_capacity
);