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