xref: /openbmc/linux/drivers/base/arch_topology.c (revision 1b39eacd)
1 /*
2  * Arch specific cpu topology information
3  *
4  * Copyright (C) 2016, ARM Ltd.
5  * Written by: Juri Lelli, ARM Ltd.
6  *
7  * This file is subject to the terms and conditions of the GNU General Public
8  * License.  See the file "COPYING" in the main directory of this archive
9  * for more details.
10  *
11  * Released under the GPLv2 only.
12  * SPDX-License-Identifier: GPL-2.0
13  */
14 
15 #include <linux/acpi.h>
16 #include <linux/arch_topology.h>
17 #include <linux/cpu.h>
18 #include <linux/cpufreq.h>
19 #include <linux/device.h>
20 #include <linux/of.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/sched/topology.h>
24 
25 DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
26 
27 void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
28 			 unsigned long max_freq)
29 {
30 	unsigned long scale;
31 	int i;
32 
33 	scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
34 
35 	for_each_cpu(i, cpus)
36 		per_cpu(freq_scale, i) = scale;
37 }
38 
39 static DEFINE_MUTEX(cpu_scale_mutex);
40 DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
41 
42 void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
43 {
44 	per_cpu(cpu_scale, cpu) = capacity;
45 }
46 
47 static ssize_t cpu_capacity_show(struct device *dev,
48 				 struct device_attribute *attr,
49 				 char *buf)
50 {
51 	struct cpu *cpu = container_of(dev, struct cpu, dev);
52 
53 	return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
54 }
55 
56 static ssize_t cpu_capacity_store(struct device *dev,
57 				  struct device_attribute *attr,
58 				  const char *buf,
59 				  size_t count)
60 {
61 	struct cpu *cpu = container_of(dev, struct cpu, dev);
62 	int this_cpu = cpu->dev.id;
63 	int i;
64 	unsigned long new_capacity;
65 	ssize_t ret;
66 
67 	if (!count)
68 		return 0;
69 
70 	ret = kstrtoul(buf, 0, &new_capacity);
71 	if (ret)
72 		return ret;
73 	if (new_capacity > SCHED_CAPACITY_SCALE)
74 		return -EINVAL;
75 
76 	mutex_lock(&cpu_scale_mutex);
77 	for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
78 		topology_set_cpu_scale(i, new_capacity);
79 	mutex_unlock(&cpu_scale_mutex);
80 
81 	return count;
82 }
83 
84 static DEVICE_ATTR_RW(cpu_capacity);
85 
86 static int register_cpu_capacity_sysctl(void)
87 {
88 	int i;
89 	struct device *cpu;
90 
91 	for_each_possible_cpu(i) {
92 		cpu = get_cpu_device(i);
93 		if (!cpu) {
94 			pr_err("%s: too early to get CPU%d device!\n",
95 			       __func__, i);
96 			continue;
97 		}
98 		device_create_file(cpu, &dev_attr_cpu_capacity);
99 	}
100 
101 	return 0;
102 }
103 subsys_initcall(register_cpu_capacity_sysctl);
104 
105 static u32 capacity_scale;
106 static u32 *raw_capacity;
107 
108 static int free_raw_capacity(void)
109 {
110 	kfree(raw_capacity);
111 	raw_capacity = NULL;
112 
113 	return 0;
114 }
115 
116 void topology_normalize_cpu_scale(void)
117 {
118 	u64 capacity;
119 	int cpu;
120 
121 	if (!raw_capacity)
122 		return;
123 
124 	pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
125 	mutex_lock(&cpu_scale_mutex);
126 	for_each_possible_cpu(cpu) {
127 		pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
128 			 cpu, raw_capacity[cpu]);
129 		capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
130 			/ capacity_scale;
131 		topology_set_cpu_scale(cpu, capacity);
132 		pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
133 			cpu, topology_get_cpu_scale(NULL, cpu));
134 	}
135 	mutex_unlock(&cpu_scale_mutex);
136 }
137 
138 bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
139 {
140 	static bool cap_parsing_failed;
141 	int ret;
142 	u32 cpu_capacity;
143 
144 	if (cap_parsing_failed)
145 		return false;
146 
147 	ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
148 				   &cpu_capacity);
149 	if (!ret) {
150 		if (!raw_capacity) {
151 			raw_capacity = kcalloc(num_possible_cpus(),
152 					       sizeof(*raw_capacity),
153 					       GFP_KERNEL);
154 			if (!raw_capacity) {
155 				pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
156 				cap_parsing_failed = true;
157 				return false;
158 			}
159 		}
160 		capacity_scale = max(cpu_capacity, capacity_scale);
161 		raw_capacity[cpu] = cpu_capacity;
162 		pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
163 			cpu_node, raw_capacity[cpu]);
164 	} else {
165 		if (raw_capacity) {
166 			pr_err("cpu_capacity: missing %pOF raw capacity\n",
167 				cpu_node);
168 			pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
169 		}
170 		cap_parsing_failed = true;
171 		free_raw_capacity();
172 	}
173 
174 	return !ret;
175 }
176 
177 #ifdef CONFIG_CPU_FREQ
178 static cpumask_var_t cpus_to_visit __initdata;
179 static void __init parsing_done_workfn(struct work_struct *work);
180 static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn);
181 
182 static int __init
183 init_cpu_capacity_callback(struct notifier_block *nb,
184 			   unsigned long val,
185 			   void *data)
186 {
187 	struct cpufreq_policy *policy = data;
188 	int cpu;
189 
190 	if (!raw_capacity)
191 		return 0;
192 
193 	if (val != CPUFREQ_NOTIFY)
194 		return 0;
195 
196 	pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
197 		 cpumask_pr_args(policy->related_cpus),
198 		 cpumask_pr_args(cpus_to_visit));
199 
200 	cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
201 
202 	for_each_cpu(cpu, policy->related_cpus) {
203 		raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
204 				    policy->cpuinfo.max_freq / 1000UL;
205 		capacity_scale = max(raw_capacity[cpu], capacity_scale);
206 	}
207 
208 	if (cpumask_empty(cpus_to_visit)) {
209 		topology_normalize_cpu_scale();
210 		free_raw_capacity();
211 		pr_debug("cpu_capacity: parsing done\n");
212 		schedule_work(&parsing_done_work);
213 	}
214 
215 	return 0;
216 }
217 
218 static struct notifier_block init_cpu_capacity_notifier __initdata = {
219 	.notifier_call = init_cpu_capacity_callback,
220 };
221 
222 static int __init register_cpufreq_notifier(void)
223 {
224 	int ret;
225 
226 	/*
227 	 * on ACPI-based systems we need to use the default cpu capacity
228 	 * until we have the necessary code to parse the cpu capacity, so
229 	 * skip registering cpufreq notifier.
230 	 */
231 	if (!acpi_disabled || !raw_capacity)
232 		return -EINVAL;
233 
234 	if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
235 		pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
236 		return -ENOMEM;
237 	}
238 
239 	cpumask_copy(cpus_to_visit, cpu_possible_mask);
240 
241 	ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
242 					CPUFREQ_POLICY_NOTIFIER);
243 
244 	if (ret)
245 		free_cpumask_var(cpus_to_visit);
246 
247 	return ret;
248 }
249 core_initcall(register_cpufreq_notifier);
250 
251 static void __init parsing_done_workfn(struct work_struct *work)
252 {
253 	cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
254 					 CPUFREQ_POLICY_NOTIFIER);
255 	free_cpumask_var(cpus_to_visit);
256 }
257 
258 #else
259 core_initcall(free_raw_capacity);
260 #endif
261