Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1/*
  2 * arch/arm64/kernel/topology.c
  3 *
  4 * Copyright (C) 2011,2013,2014 Linaro Limited.
  5 *
  6 * Based on the arm32 version written by Vincent Guittot in turn based on
  7 * arch/sh/kernel/topology.c
  8 *
  9 * This file is subject to the terms and conditions of the GNU General Public
 10 * License.  See the file "COPYING" in the main directory of this archive
 11 * for more details.
 12 */
 13
 14#include <linux/acpi.h>
 15#include <linux/cpu.h>
 16#include <linux/cpumask.h>
 17#include <linux/init.h>
 18#include <linux/percpu.h>
 19#include <linux/node.h>
 20#include <linux/nodemask.h>
 21#include <linux/of.h>
 22#include <linux/sched.h>
 23#include <linux/slab.h>
 24#include <linux/string.h>
 25#include <linux/cpufreq.h>
 26
 27#include <asm/cpu.h>
 28#include <asm/cputype.h>
 29#include <asm/topology.h>
 30
 31static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
 32static DEFINE_MUTEX(cpu_scale_mutex);
 33
 34unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
 35{
 36	return per_cpu(cpu_scale, cpu);
 37}
 38
 39static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
 40{
 41	per_cpu(cpu_scale, cpu) = capacity;
 42}
 43
 44#ifdef CONFIG_PROC_SYSCTL
 45static ssize_t cpu_capacity_show(struct device *dev,
 46				 struct device_attribute *attr,
 47				 char *buf)
 48{
 49	struct cpu *cpu = container_of(dev, struct cpu, dev);
 50
 51	return sprintf(buf, "%lu\n",
 52			arch_scale_cpu_capacity(NULL, cpu->dev.id));
 53}
 54
 55static ssize_t cpu_capacity_store(struct device *dev,
 56				  struct device_attribute *attr,
 57				  const char *buf,
 58				  size_t count)
 59{
 60	struct cpu *cpu = container_of(dev, struct cpu, dev);
 61	int this_cpu = cpu->dev.id, i;
 62	unsigned long new_capacity;
 63	ssize_t ret;
 64
 65	if (count) {
 66		ret = kstrtoul(buf, 0, &new_capacity);
 67		if (ret)
 68			return ret;
 69		if (new_capacity > SCHED_CAPACITY_SCALE)
 70			return -EINVAL;
 71
 72		mutex_lock(&cpu_scale_mutex);
 73		for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
 74			set_capacity_scale(i, new_capacity);
 75		mutex_unlock(&cpu_scale_mutex);
 76	}
 77
 78	return count;
 79}
 80
 81static DEVICE_ATTR_RW(cpu_capacity);
 82
 83static int register_cpu_capacity_sysctl(void)
 84{
 85	int i;
 86	struct device *cpu;
 87
 88	for_each_possible_cpu(i) {
 89		cpu = get_cpu_device(i);
 90		if (!cpu) {
 91			pr_err("%s: too early to get CPU%d device!\n",
 92			       __func__, i);
 93			continue;
 94		}
 95		device_create_file(cpu, &dev_attr_cpu_capacity);
 96	}
 97
 98	return 0;
 99}
100subsys_initcall(register_cpu_capacity_sysctl);
101#endif
102
103static u32 capacity_scale;
104static u32 *raw_capacity;
105static bool cap_parsing_failed;
106
107static void __init parse_cpu_capacity(struct device_node *cpu_node, int cpu)
108{
109	int ret;
110	u32 cpu_capacity;
111
112	if (cap_parsing_failed)
113		return;
114
115	ret = of_property_read_u32(cpu_node,
116				   "capacity-dmips-mhz",
117				   &cpu_capacity);
118	if (!ret) {
119		if (!raw_capacity) {
120			raw_capacity = kcalloc(num_possible_cpus(),
121					       sizeof(*raw_capacity),
122					       GFP_KERNEL);
123			if (!raw_capacity) {
124				pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
125				cap_parsing_failed = true;
126				return;
127			}
128		}
129		capacity_scale = max(cpu_capacity, capacity_scale);
130		raw_capacity[cpu] = cpu_capacity;
131		pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
132			cpu_node->full_name, raw_capacity[cpu]);
133	} else {
134		if (raw_capacity) {
135			pr_err("cpu_capacity: missing %s raw capacity\n",
136				cpu_node->full_name);
137			pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
138		}
139		cap_parsing_failed = true;
140		kfree(raw_capacity);
141	}
142}
143
144static void normalize_cpu_capacity(void)
145{
146	u64 capacity;
147	int cpu;
148
149	if (!raw_capacity || cap_parsing_failed)
150		return;
151
152	pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
153	mutex_lock(&cpu_scale_mutex);
154	for_each_possible_cpu(cpu) {
155		pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
156			 cpu, raw_capacity[cpu]);
157		capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
158			/ capacity_scale;
159		set_capacity_scale(cpu, capacity);
160		pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
161			cpu, arch_scale_cpu_capacity(NULL, cpu));
162	}
163	mutex_unlock(&cpu_scale_mutex);
164}
165
166#ifdef CONFIG_CPU_FREQ
167static cpumask_var_t cpus_to_visit;
168static bool cap_parsing_done;
169static void parsing_done_workfn(struct work_struct *work);
170static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
171
172static int
173init_cpu_capacity_callback(struct notifier_block *nb,
174			   unsigned long val,
175			   void *data)
176{
177	struct cpufreq_policy *policy = data;
178	int cpu;
179
180	if (cap_parsing_failed || cap_parsing_done)
181		return 0;
182
183	switch (val) {
184	case CPUFREQ_NOTIFY:
185		pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
186				cpumask_pr_args(policy->related_cpus),
187				cpumask_pr_args(cpus_to_visit));
188		cpumask_andnot(cpus_to_visit,
189			       cpus_to_visit,
190			       policy->related_cpus);
191		for_each_cpu(cpu, policy->related_cpus) {
192			raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) *
193					    policy->cpuinfo.max_freq / 1000UL;
194			capacity_scale = max(raw_capacity[cpu], capacity_scale);
195		}
196		if (cpumask_empty(cpus_to_visit)) {
197			normalize_cpu_capacity();
198			kfree(raw_capacity);
199			pr_debug("cpu_capacity: parsing done\n");
200			cap_parsing_done = true;
201			schedule_work(&parsing_done_work);
202		}
203	}
204	return 0;
205}
206
207static struct notifier_block init_cpu_capacity_notifier = {
208	.notifier_call = init_cpu_capacity_callback,
209};
210
211static int __init register_cpufreq_notifier(void)
212{
213	/*
214	 * on ACPI-based systems we need to use the default cpu capacity
215	 * until we have the necessary code to parse the cpu capacity, so
216	 * skip registering cpufreq notifier.
217	 */
218	if (!acpi_disabled || cap_parsing_failed)
219		return -EINVAL;
220
221	if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
222		pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
223		return -ENOMEM;
224	}
225	cpumask_copy(cpus_to_visit, cpu_possible_mask);
226
227	return cpufreq_register_notifier(&init_cpu_capacity_notifier,
228					 CPUFREQ_POLICY_NOTIFIER);
229}
230core_initcall(register_cpufreq_notifier);
231
232static void parsing_done_workfn(struct work_struct *work)
233{
234	cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
235					 CPUFREQ_POLICY_NOTIFIER);
236}
237
238#else
239static int __init free_raw_capacity(void)
240{
241	kfree(raw_capacity);
242
243	return 0;
244}
245core_initcall(free_raw_capacity);
246#endif
247
248static int __init get_cpu_for_node(struct device_node *node)
249{
250	struct device_node *cpu_node;
251	int cpu;
252
253	cpu_node = of_parse_phandle(node, "cpu", 0);
254	if (!cpu_node)
255		return -1;
256
257	for_each_possible_cpu(cpu) {
258		if (of_get_cpu_node(cpu, NULL) == cpu_node) {
259			parse_cpu_capacity(cpu_node, cpu);
260			of_node_put(cpu_node);
261			return cpu;
262		}
263	}
264
265	pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
266
267	of_node_put(cpu_node);
268	return -1;
269}
270
271static int __init parse_core(struct device_node *core, int cluster_id,
272			     int core_id)
273{
274	char name[10];
275	bool leaf = true;
276	int i = 0;
277	int cpu;
278	struct device_node *t;
279
280	do {
281		snprintf(name, sizeof(name), "thread%d", i);
282		t = of_get_child_by_name(core, name);
283		if (t) {
284			leaf = false;
285			cpu = get_cpu_for_node(t);
286			if (cpu >= 0) {
287				cpu_topology[cpu].cluster_id = cluster_id;
288				cpu_topology[cpu].core_id = core_id;
289				cpu_topology[cpu].thread_id = i;
290			} else {
291				pr_err("%s: Can't get CPU for thread\n",
292				       t->full_name);
293				of_node_put(t);
294				return -EINVAL;
295			}
296			of_node_put(t);
297		}
298		i++;
299	} while (t);
300
301	cpu = get_cpu_for_node(core);
302	if (cpu >= 0) {
303		if (!leaf) {
304			pr_err("%s: Core has both threads and CPU\n",
305			       core->full_name);
306			return -EINVAL;
307		}
308
309		cpu_topology[cpu].cluster_id = cluster_id;
310		cpu_topology[cpu].core_id = core_id;
311	} else if (leaf) {
312		pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
313		return -EINVAL;
314	}
315
316	return 0;
317}
318
319static int __init parse_cluster(struct device_node *cluster, int depth)
320{
321	char name[10];
322	bool leaf = true;
323	bool has_cores = false;
324	struct device_node *c;
325	static int cluster_id __initdata;
326	int core_id = 0;
327	int i, ret;
328
329	/*
330	 * First check for child clusters; we currently ignore any
331	 * information about the nesting of clusters and present the
332	 * scheduler with a flat list of them.
333	 */
334	i = 0;
335	do {
336		snprintf(name, sizeof(name), "cluster%d", i);
337		c = of_get_child_by_name(cluster, name);
338		if (c) {
339			leaf = false;
340			ret = parse_cluster(c, depth + 1);
341			of_node_put(c);
342			if (ret != 0)
343				return ret;
344		}
345		i++;
346	} while (c);
347
348	/* Now check for cores */
349	i = 0;
350	do {
351		snprintf(name, sizeof(name), "core%d", i);
352		c = of_get_child_by_name(cluster, name);
353		if (c) {
354			has_cores = true;
355
356			if (depth == 0) {
357				pr_err("%s: cpu-map children should be clusters\n",
358				       c->full_name);
359				of_node_put(c);
360				return -EINVAL;
361			}
362
363			if (leaf) {
364				ret = parse_core(c, cluster_id, core_id++);
365			} else {
366				pr_err("%s: Non-leaf cluster with core %s\n",
367				       cluster->full_name, name);
368				ret = -EINVAL;
369			}
370
371			of_node_put(c);
372			if (ret != 0)
373				return ret;
374		}
375		i++;
376	} while (c);
377
378	if (leaf && !has_cores)
379		pr_warn("%s: empty cluster\n", cluster->full_name);
380
381	if (leaf)
382		cluster_id++;
383
384	return 0;
385}
386
387static int __init parse_dt_topology(void)
388{
389	struct device_node *cn, *map;
390	int ret = 0;
391	int cpu;
392
393	cn = of_find_node_by_path("/cpus");
394	if (!cn) {
395		pr_err("No CPU information found in DT\n");
396		return 0;
397	}
398
399	/*
400	 * When topology is provided cpu-map is essentially a root
401	 * cluster with restricted subnodes.
402	 */
403	map = of_get_child_by_name(cn, "cpu-map");
404	if (!map) {
405		cap_parsing_failed = true;
406		goto out;
407	}
408
409	ret = parse_cluster(map, 0);
410	if (ret != 0)
411		goto out_map;
412
413	normalize_cpu_capacity();
414
415	/*
416	 * Check that all cores are in the topology; the SMP code will
417	 * only mark cores described in the DT as possible.
418	 */
419	for_each_possible_cpu(cpu)
420		if (cpu_topology[cpu].cluster_id == -1)
421			ret = -EINVAL;
422
423out_map:
424	of_node_put(map);
425out:
426	of_node_put(cn);
427	return ret;
428}
429
430/*
431 * cpu topology table
432 */
433struct cpu_topology cpu_topology[NR_CPUS];
434EXPORT_SYMBOL_GPL(cpu_topology);
435
436const struct cpumask *cpu_coregroup_mask(int cpu)
437{
438	return &cpu_topology[cpu].core_sibling;
439}
440
441static void update_siblings_masks(unsigned int cpuid)
442{
443	struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
444	int cpu;
445
446	/* update core and thread sibling masks */
447	for_each_possible_cpu(cpu) {
448		cpu_topo = &cpu_topology[cpu];
449
450		if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
451			continue;
452
453		cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
454		if (cpu != cpuid)
455			cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
456
457		if (cpuid_topo->core_id != cpu_topo->core_id)
458			continue;
459
460		cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
461		if (cpu != cpuid)
462			cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
463	}
464}
465
466void store_cpu_topology(unsigned int cpuid)
467{
468	struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
469	u64 mpidr;
470
471	if (cpuid_topo->cluster_id != -1)
472		goto topology_populated;
473
474	mpidr = read_cpuid_mpidr();
475
476	/* Uniprocessor systems can rely on default topology values */
477	if (mpidr & MPIDR_UP_BITMASK)
478		return;
479
480	/* Create cpu topology mapping based on MPIDR. */
481	if (mpidr & MPIDR_MT_BITMASK) {
482		/* Multiprocessor system : Multi-threads per core */
483		cpuid_topo->thread_id  = MPIDR_AFFINITY_LEVEL(mpidr, 0);
484		cpuid_topo->core_id    = MPIDR_AFFINITY_LEVEL(mpidr, 1);
485		cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
486					 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
487	} else {
488		/* Multiprocessor system : Single-thread per core */
489		cpuid_topo->thread_id  = -1;
490		cpuid_topo->core_id    = MPIDR_AFFINITY_LEVEL(mpidr, 0);
491		cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
492					 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
493					 MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
494	}
495
496	pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
497		 cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
498		 cpuid_topo->thread_id, mpidr);
499
500topology_populated:
501	update_siblings_masks(cpuid);
502}
503
504static void __init reset_cpu_topology(void)
505{
506	unsigned int cpu;
507
508	for_each_possible_cpu(cpu) {
509		struct cpu_topology *cpu_topo = &cpu_topology[cpu];
510
511		cpu_topo->thread_id = -1;
512		cpu_topo->core_id = 0;
513		cpu_topo->cluster_id = -1;
514
515		cpumask_clear(&cpu_topo->core_sibling);
516		cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
517		cpumask_clear(&cpu_topo->thread_sibling);
518		cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
519	}
520}
521
522void __init init_cpu_topology(void)
523{
524	reset_cpu_topology();
525
526	/*
527	 * Discard anything that was parsed if we hit an error so we
528	 * don't use partial information.
529	 */
530	if (of_have_populated_dt() && parse_dt_topology())
531		reset_cpu_topology();
532}