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/arch_topology.h>
11 #include <linux/cpu.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
14 #include <linux/of.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/sched/topology.h>
18 
19 DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
20 
arch_set_freq_scale(struct cpumask * cpus,unsigned long cur_freq,unsigned long max_freq)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 static DEFINE_MUTEX(cpu_scale_mutex);
34 DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
35 
topology_set_cpu_scale(unsigned int cpu,unsigned long capacity)36 void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
37 {
38 	per_cpu(cpu_scale, cpu) = capacity;
39 }
40 
cpu_capacity_show(struct device * dev,struct device_attribute * attr,char * buf)41 static ssize_t cpu_capacity_show(struct device *dev,
42 				 struct device_attribute *attr,
43 				 char *buf)
44 {
45 	struct cpu *cpu = container_of(dev, struct cpu, dev);
46 
47 	return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
48 }
49 
cpu_capacity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)50 static ssize_t cpu_capacity_store(struct device *dev,
51 				  struct device_attribute *attr,
52 				  const char *buf,
53 				  size_t count)
54 {
55 	struct cpu *cpu = container_of(dev, struct cpu, dev);
56 	int this_cpu = cpu->dev.id;
57 	int i;
58 	unsigned long new_capacity;
59 	ssize_t ret;
60 
61 	if (!count)
62 		return 0;
63 
64 	ret = kstrtoul(buf, 0, &new_capacity);
65 	if (ret)
66 		return ret;
67 	if (new_capacity > SCHED_CAPACITY_SCALE)
68 		return -EINVAL;
69 
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);
74 
75 	return count;
76 }
77 
78 static DEVICE_ATTR_RW(cpu_capacity);
79 
register_cpu_capacity_sysctl(void)80 static int register_cpu_capacity_sysctl(void)
81 {
82 	int i;
83 	struct device *cpu;
84 
85 	for_each_possible_cpu(i) {
86 		cpu = get_cpu_device(i);
87 		if (!cpu) {
88 			pr_err("%s: too early to get CPU%d device!\n",
89 			       __func__, i);
90 			continue;
91 		}
92 		device_create_file(cpu, &dev_attr_cpu_capacity);
93 	}
94 
95 	return 0;
96 }
97 subsys_initcall(register_cpu_capacity_sysctl);
98 
99 static u32 capacity_scale;
100 static u32 *raw_capacity;
101 
free_raw_capacity(void)102 static int free_raw_capacity(void)
103 {
104 	kfree(raw_capacity);
105 	raw_capacity = NULL;
106 
107 	return 0;
108 }
109 
topology_normalize_cpu_scale(void)110 void topology_normalize_cpu_scale(void)
111 {
112 	u64 capacity;
113 	int cpu;
114 
115 	if (!raw_capacity)
116 		return;
117 
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)
124 			/ capacity_scale;
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));
128 	}
129 	mutex_unlock(&cpu_scale_mutex);
130 }
131 
topology_parse_cpu_capacity(struct device_node * cpu_node,int cpu)132 bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
133 {
134 	static bool cap_parsing_failed;
135 	int ret;
136 	u32 cpu_capacity;
137 
138 	if (cap_parsing_failed)
139 		return false;
140 
141 	ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
142 				   &cpu_capacity);
143 	if (!ret) {
144 		if (!raw_capacity) {
145 			raw_capacity = kcalloc(num_possible_cpus(),
146 					       sizeof(*raw_capacity),
147 					       GFP_KERNEL);
148 			if (!raw_capacity) {
149 				pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
150 				cap_parsing_failed = true;
151 				return false;
152 			}
153 		}
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]);
158 	} else {
159 		if (raw_capacity) {
160 			pr_err("cpu_capacity: missing %pOF raw capacity\n",
161 				cpu_node);
162 			pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
163 		}
164 		cap_parsing_failed = true;
165 		free_raw_capacity();
166 	}
167 
168 	return !ret;
169 }
170 
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);
175 
176 static int
init_cpu_capacity_callback(struct notifier_block * nb,unsigned long val,void * data)177 init_cpu_capacity_callback(struct notifier_block *nb,
178 			   unsigned long val,
179 			   void *data)
180 {
181 	struct cpufreq_policy *policy = data;
182 	int cpu;
183 
184 	if (!raw_capacity)
185 		return 0;
186 
187 	if (val != CPUFREQ_NOTIFY)
188 		return 0;
189 
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));
193 
194 	cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
195 
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);
200 	}
201 
202 	if (cpumask_empty(cpus_to_visit)) {
203 		topology_normalize_cpu_scale();
204 		free_raw_capacity();
205 		pr_debug("cpu_capacity: parsing done\n");
206 		schedule_work(&parsing_done_work);
207 	}
208 
209 	return 0;
210 }
211 
212 static struct notifier_block init_cpu_capacity_notifier = {
213 	.notifier_call = init_cpu_capacity_callback,
214 };
215 
register_cpufreq_notifier(void)216 static int __init register_cpufreq_notifier(void)
217 {
218 	int ret;
219 
220 	/*
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.
224 	 */
225 	if (!acpi_disabled || !raw_capacity)
226 		return -EINVAL;
227 
228 	if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
229 		pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
230 		return -ENOMEM;
231 	}
232 
233 	cpumask_copy(cpus_to_visit, cpu_possible_mask);
234 
235 	ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
236 					CPUFREQ_POLICY_NOTIFIER);
237 
238 	if (ret)
239 		free_cpumask_var(cpus_to_visit);
240 
241 	return ret;
242 }
243 core_initcall(register_cpufreq_notifier);
244 
parsing_done_workfn(struct work_struct * work)245 static void parsing_done_workfn(struct work_struct *work)
246 {
247 	cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
248 					 CPUFREQ_POLICY_NOTIFIER);
249 	free_cpumask_var(cpus_to_visit);
250 }
251 
252 #else
253 core_initcall(free_raw_capacity);
254 #endif
255