Skip to content

Commit 2ef7a29

Browse files
Juri Lelligregkh
authored andcommitted
arm, arm64: factorize common cpu capacity default code
arm and arm64 share lot of code relative to parsing CPU capacity information from DT, using that information for appropriate scaling and exposing a sysfs interface for chaging such values at runtime. Factorize such code in a common place (driver/base/arch_topology.c) in preparation for further additions. Suggested-by: Will Deacon <will.deacon@arm.com> Suggested-by: Mark Rutland <mark.rutland@arm.com> Suggested-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Russell King <linux@armlinux.org.uk> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Juri Lelli <juri.lelli@arm.com> Acked-by: Russell King <rmk+kernel@armlinux.org.uk> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f70b281 commit 2ef7a29

7 files changed

Lines changed: 262 additions & 423 deletions

File tree

‎arch/arm/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ config ARM
2525
select EDAC_SUPPORT
2626
select EDAC_ATOMIC_SCRUB
2727
select GENERIC_ALLOCATOR
28+
select GENERIC_ARCH_TOPOLOGY if ARM_CPU_TOPOLOGY
2829
select GENERIC_ATOMIC64 if (CPU_V7M || CPU_V6 || !CPU_32v6K || !AEABI)
2930
select GENERIC_CLOCKEVENTS_BROADCAST if SMP
3031
select GENERIC_CPU_AUTOPROBE

‎arch/arm/kernel/topology.c‎

Lines changed: 6 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -44,75 +44,10 @@
4444
* to run the rebalance_domains for all idle cores and the cpu_capacity can be
4545
* updated during this sequence.
4646
*/
47-
static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
48-
static DEFINE_MUTEX(cpu_scale_mutex);
4947

50-
unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
51-
{
52-
return per_cpu(cpu_scale, cpu);
53-
}
54-
55-
static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
56-
{
57-
per_cpu(cpu_scale, cpu) = capacity;
58-
}
59-
60-
static ssize_t cpu_capacity_show(struct device *dev,
61-
struct device_attribute *attr,
62-
char *buf)
63-
{
64-
struct cpu *cpu = container_of(dev, struct cpu, dev);
65-
66-
return sprintf(buf, "%lu\n",
67-
arch_scale_cpu_capacity(NULL, cpu->dev.id));
68-
}
69-
70-
static ssize_t cpu_capacity_store(struct device *dev,
71-
struct device_attribute *attr,
72-
const char *buf,
73-
size_t count)
74-
{
75-
struct cpu *cpu = container_of(dev, struct cpu, dev);
76-
int this_cpu = cpu->dev.id, i;
77-
unsigned long new_capacity;
78-
ssize_t ret;
79-
80-
if (count) {
81-
ret = kstrtoul(buf, 0, &new_capacity);
82-
if (ret)
83-
return ret;
84-
if (new_capacity > SCHED_CAPACITY_SCALE)
85-
return -EINVAL;
86-
87-
mutex_lock(&cpu_scale_mutex);
88-
for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
89-
set_capacity_scale(i, new_capacity);
90-
mutex_unlock(&cpu_scale_mutex);
91-
}
92-
93-
return count;
94-
}
95-
96-
static DEVICE_ATTR_RW(cpu_capacity);
97-
98-
static int register_cpu_capacity_sysctl(void)
99-
{
100-
int i;
101-
struct device *cpu;
102-
103-
for_each_possible_cpu(i) {
104-
cpu = get_cpu_device(i);
105-
if (!cpu) {
106-
pr_err("%s: too early to get CPU%d device!\n",
107-
__func__, i);
108-
continue;
109-
}
110-
device_create_file(cpu, &dev_attr_cpu_capacity);
111-
}
112-
113-
return 0;
114-
}
115-
subsys_initcall(register_cpu_capacity_sysctl);
48+
extern unsigned long
49+
arch_scale_cpu_capacity(struct sched_domain *sd, int cpu);
50+
extern void set_capacity_scale(unsigned int cpu, unsigned long capacity);
11651

11752
#ifdef CONFIG_OF
11853
struct cpu_efficiency {
@@ -141,145 +76,9 @@ static unsigned long *__cpu_capacity;
14176

14277
static unsigned long middle_capacity = 1;
14378
static bool cap_from_dt = true;
144-
static u32 *raw_capacity;
145-
static bool cap_parsing_failed;
146-
static u32 capacity_scale;
147-
148-
static int __init parse_cpu_capacity(struct device_node *cpu_node, int cpu)
149-
{
150-
int ret = 1;
151-
u32 cpu_capacity;
152-
153-
if (cap_parsing_failed)
154-
return !ret;
155-
156-
ret = of_property_read_u32(cpu_node,
157-
"capacity-dmips-mhz",
158-
&cpu_capacity);
159-
if (!ret) {
160-
if (!raw_capacity) {
161-
raw_capacity = kcalloc(num_possible_cpus(),
162-
sizeof(*raw_capacity),
163-
GFP_KERNEL);
164-
if (!raw_capacity) {
165-
pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
166-
cap_parsing_failed = true;
167-
return 0;
168-
}
169-
}
170-
capacity_scale = max(cpu_capacity, capacity_scale);
171-
raw_capacity[cpu] = cpu_capacity;
172-
pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
173-
cpu_node->full_name, raw_capacity[cpu]);
174-
} else {
175-
if (raw_capacity) {
176-
pr_err("cpu_capacity: missing %s raw capacity\n",
177-
cpu_node->full_name);
178-
pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
179-
}
180-
cap_parsing_failed = true;
181-
kfree(raw_capacity);
182-
}
183-
184-
return !ret;
185-
}
186-
187-
static void normalize_cpu_capacity(void)
188-
{
189-
u64 capacity;
190-
int cpu;
191-
192-
if (!raw_capacity || cap_parsing_failed)
193-
return;
194-
195-
pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
196-
mutex_lock(&cpu_scale_mutex);
197-
for_each_possible_cpu(cpu) {
198-
capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
199-
/ capacity_scale;
200-
set_capacity_scale(cpu, capacity);
201-
pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
202-
cpu, arch_scale_cpu_capacity(NULL, cpu));
203-
}
204-
mutex_unlock(&cpu_scale_mutex);
205-
}
206-
207-
#ifdef CONFIG_CPU_FREQ
208-
static cpumask_var_t cpus_to_visit;
209-
static bool cap_parsing_done;
210-
static void parsing_done_workfn(struct work_struct *work);
211-
static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
212-
213-
static int
214-
init_cpu_capacity_callback(struct notifier_block *nb,
215-
unsigned long val,
216-
void *data)
217-
{
218-
struct cpufreq_policy *policy = data;
219-
int cpu;
220-
221-
if (cap_parsing_failed || cap_parsing_done)
222-
return 0;
223-
224-
switch (val) {
225-
case CPUFREQ_NOTIFY:
226-
pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
227-
cpumask_pr_args(policy->related_cpus),
228-
cpumask_pr_args(cpus_to_visit));
229-
cpumask_andnot(cpus_to_visit,
230-
cpus_to_visit,
231-
policy->related_cpus);
232-
for_each_cpu(cpu, policy->related_cpus) {
233-
raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) *
234-
policy->cpuinfo.max_freq / 1000UL;
235-
capacity_scale = max(raw_capacity[cpu], capacity_scale);
236-
}
237-
if (cpumask_empty(cpus_to_visit)) {
238-
normalize_cpu_capacity();
239-
kfree(raw_capacity);
240-
pr_debug("cpu_capacity: parsing done\n");
241-
cap_parsing_done = true;
242-
schedule_work(&parsing_done_work);
243-
}
244-
}
245-
return 0;
246-
}
247-
248-
static struct notifier_block init_cpu_capacity_notifier = {
249-
.notifier_call = init_cpu_capacity_callback,
250-
};
251-
252-
static int __init register_cpufreq_notifier(void)
253-
{
254-
if (cap_parsing_failed)
255-
return -EINVAL;
256-
257-
if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
258-
pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
259-
return -ENOMEM;
260-
}
261-
cpumask_copy(cpus_to_visit, cpu_possible_mask);
262-
263-
return cpufreq_register_notifier(&init_cpu_capacity_notifier,
264-
CPUFREQ_POLICY_NOTIFIER);
265-
}
266-
core_initcall(register_cpufreq_notifier);
267-
268-
static void parsing_done_workfn(struct work_struct *work)
269-
{
270-
cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
271-
CPUFREQ_POLICY_NOTIFIER);
272-
}
273-
274-
#else
275-
static int __init free_raw_capacity(void)
276-
{
277-
kfree(raw_capacity);
278-
279-
return 0;
280-
}
281-
core_initcall(free_raw_capacity);
282-
#endif
79+
extern bool cap_parsing_failed;
80+
extern void normalize_cpu_capacity(void);
81+
extern int __init parse_cpu_capacity(struct device_node *cpu_node, int cpu);
28382

28483
/*
28584
* Iterate all CPUs' descriptor in DT and compute the efficiency

‎arch/arm64/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ config ARM64
4141
select EDAC_SUPPORT
4242
select FRAME_POINTER
4343
select GENERIC_ALLOCATOR
44+
select GENERIC_ARCH_TOPOLOGY
4445
select GENERIC_CLOCKEVENTS
4546
select GENERIC_CLOCKEVENTS_BROADCAST
4647
select GENERIC_CPU_AUTOPROBE

0 commit comments

Comments
 (0)