ARM: rockchip: rk3228: implement function rk3228_restart
[firefly-linux-kernel-4.4.55.git] / arch / arm / kernel / topology.c
1 /*
2  * arch/arm/kernel/topology.c
3  *
4  * Copyright (C) 2011 Linaro Limited.
5  * Written by: Vincent Guittot
6  *
7  * based on 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/cpu.h>
15 #include <linux/cpumask.h>
16 #include <linux/export.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
25 #include <asm/cputype.h>
26 #include <asm/smp_plat.h>
27 #include <asm/topology.h>
28
29 /*
30  * cpu power scale management
31  */
32
33 /*
34  * cpu power table
35  * This per cpu data structure describes the relative capacity of each core.
36  * On a heteregenous system, cores don't have the same computation capacity
37  * and we reflect that difference in the cpu_power field so the scheduler can
38  * take this difference into account during load balance. A per cpu structure
39  * is preferred because each CPU updates its own cpu_power field during the
40  * load balance except for idle cores. One idle core is selected to run the
41  * rebalance_domains for all idle cores and the cpu_power can be updated
42  * during this sequence.
43  */
44 static DEFINE_PER_CPU(unsigned long, cpu_scale);
45
46 unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
47 {
48         return per_cpu(cpu_scale, cpu);
49 }
50
51 static void set_power_scale(unsigned int cpu, unsigned long power)
52 {
53         per_cpu(cpu_scale, cpu) = power;
54 }
55
56 #ifdef CONFIG_OF
57 struct cpu_efficiency {
58         const char *compatible;
59         unsigned long efficiency;
60 };
61
62 /*
63  * Table of relative efficiency of each processors
64  * The efficiency value must fit in 20bit and the final
65  * cpu_scale value must be in the range
66  *   0 < cpu_scale < 3*SCHED_POWER_SCALE/2
67  * in order to return at most 1 when DIV_ROUND_CLOSEST
68  * is used to compute the capacity of a CPU.
69  * Processors that are not defined in the table,
70  * use the default SCHED_POWER_SCALE value for cpu_scale.
71  */
72 struct cpu_efficiency table_efficiency[] = {
73         {"arm,cortex-a15", 3891},
74         {"arm,cortex-a7",  2048},
75         {NULL, },
76 };
77
78 struct cpu_capacity {
79         unsigned long hwid;
80         unsigned long capacity;
81 };
82
83 struct cpu_capacity *cpu_capacity;
84
85 unsigned long middle_capacity = 1;
86
87 /*
88  * Iterate all CPUs' descriptor in DT and compute the efficiency
89  * (as per table_efficiency). Also calculate a middle efficiency
90  * as close as possible to  (max{eff_i} - min{eff_i}) / 2
91  * This is later used to scale the cpu_power field such that an
92  * 'average' CPU is of middle power. Also see the comments near
93  * table_efficiency[] and update_cpu_power().
94  */
95 static void __init parse_dt_topology(void)
96 {
97         struct cpu_efficiency *cpu_eff;
98         struct device_node *cn = NULL;
99         unsigned long min_capacity = (unsigned long)(-1);
100         unsigned long max_capacity = 0;
101         unsigned long capacity = 0;
102         int alloc_size, cpu = 0;
103
104         alloc_size = nr_cpu_ids * sizeof(struct cpu_capacity);
105         cpu_capacity = kzalloc(alloc_size, GFP_NOWAIT);
106
107         while ((cn = of_find_node_by_type(cn, "cpu"))) {
108                 const u32 *rate, *reg;
109                 int len;
110
111                 if (cpu >= num_possible_cpus())
112                         break;
113
114                 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
115                         if (of_device_is_compatible(cn, cpu_eff->compatible))
116                                 break;
117
118                 if (cpu_eff->compatible == NULL)
119                         continue;
120
121                 rate = of_get_property(cn, "clock-frequency", &len);
122                 if (!rate || len != 4) {
123                         pr_err("%s missing clock-frequency property\n",
124                                 cn->full_name);
125                         continue;
126                 }
127
128                 reg = of_get_property(cn, "reg", &len);
129                 if (!reg || len != 4) {
130                         pr_err("%s missing reg property\n", cn->full_name);
131                         continue;
132                 }
133
134                 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
135
136                 /* Save min capacity of the system */
137                 if (capacity < min_capacity)
138                         min_capacity = capacity;
139
140                 /* Save max capacity of the system */
141                 if (capacity > max_capacity)
142                         max_capacity = capacity;
143
144                 cpu_capacity[cpu].capacity = capacity;
145                 cpu_capacity[cpu++].hwid = be32_to_cpup(reg);
146         }
147
148         if (cpu < num_possible_cpus())
149                 cpu_capacity[cpu].hwid = (unsigned long)(-1);
150
151         /* If min and max capacities are equals, we bypass the update of the
152          * cpu_scale because all CPUs have the same capacity. Otherwise, we
153          * compute a middle_capacity factor that will ensure that the capacity
154          * of an 'average' CPU of the system will be as close as possible to
155          * SCHED_POWER_SCALE, which is the default value, but with the
156          * constraint explained near table_efficiency[].
157          */
158         if (min_capacity == max_capacity)
159                 cpu_capacity[0].hwid = (unsigned long)(-1);
160         else if (4*max_capacity < (3*(max_capacity + min_capacity)))
161                 middle_capacity = (min_capacity + max_capacity)
162                                 >> (SCHED_POWER_SHIFT+1);
163         else
164                 middle_capacity = ((max_capacity / 3)
165                                 >> (SCHED_POWER_SHIFT-1)) + 1;
166
167 }
168
169 /*
170  * Look for a customed capacity of a CPU in the cpu_capacity table during the
171  * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
172  * function returns directly for SMP system.
173  */
174 void update_cpu_power(unsigned int cpu, unsigned long hwid)
175 {
176         unsigned int idx = 0;
177
178         /* look for the cpu's hwid in the cpu capacity table */
179         for (idx = 0; idx < num_possible_cpus(); idx++) {
180                 if (cpu_capacity[idx].hwid == hwid)
181                         break;
182
183                 if (cpu_capacity[idx].hwid == -1)
184                         return;
185         }
186
187         if (idx == num_possible_cpus())
188                 return;
189
190         set_power_scale(cpu, cpu_capacity[idx].capacity / middle_capacity);
191
192         printk(KERN_INFO "CPU%u: update cpu_power %lu\n",
193                 cpu, arch_scale_freq_power(NULL, cpu));
194 }
195
196 #else
197 static inline void parse_dt_topology(void) {}
198 static inline void update_cpu_power(unsigned int cpuid, unsigned int mpidr) {}
199 #endif
200
201  /*
202  * cpu topology table
203  */
204 struct cputopo_arm cpu_topology[NR_CPUS];
205 EXPORT_SYMBOL_GPL(cpu_topology);
206
207 const struct cpumask *cpu_coregroup_mask(int cpu)
208 {
209         return &cpu_topology[cpu].core_sibling;
210 }
211
212 void update_siblings_masks(unsigned int cpuid)
213 {
214         struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
215         int cpu;
216
217         /* update core and thread sibling masks */
218         for_each_possible_cpu(cpu) {
219                 cpu_topo = &cpu_topology[cpu];
220
221                 if (cpuid_topo->socket_id != cpu_topo->socket_id)
222                         continue;
223
224                 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
225                 if (cpu != cpuid)
226                         cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
227
228                 if (cpuid_topo->core_id != cpu_topo->core_id)
229                         continue;
230
231                 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
232                 if (cpu != cpuid)
233                         cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
234         }
235         smp_wmb();
236 }
237
238 /*
239  * store_cpu_topology is called at boot when only one cpu is running
240  * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
241  * which prevents simultaneous write access to cpu_topology array
242  */
243 void store_cpu_topology(unsigned int cpuid)
244 {
245         struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
246         unsigned int mpidr;
247
248         /* If the cpu topology has been already set, just return */
249         if (cpuid_topo->core_id != -1)
250                 return;
251
252         mpidr = read_cpuid_mpidr();
253
254         /* create cpu topology mapping */
255         if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
256                 /*
257                  * This is a multiprocessor system
258                  * multiprocessor format & multiprocessor mode field are set
259                  */
260
261                 if (mpidr & MPIDR_MT_BITMASK) {
262                         /* core performance interdependency */
263                         cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
264                         cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
265                         cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
266                 } else {
267                         /* largely independent cores */
268                         cpuid_topo->thread_id = -1;
269                         cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
270                         cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
271                 }
272         } else {
273                 /*
274                  * This is an uniprocessor system
275                  * we are in multiprocessor format but uniprocessor system
276                  * or in the old uniprocessor format
277                  */
278                 cpuid_topo->thread_id = -1;
279                 cpuid_topo->core_id = 0;
280                 cpuid_topo->socket_id = -1;
281         }
282
283         update_siblings_masks(cpuid);
284
285         update_cpu_power(cpuid, mpidr & MPIDR_HWID_BITMASK);
286
287         printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
288                 cpuid, cpu_topology[cpuid].thread_id,
289                 cpu_topology[cpuid].core_id,
290                 cpu_topology[cpuid].socket_id, mpidr);
291 }
292
293
294 #ifdef CONFIG_SCHED_HMP
295
296 static const char * const little_cores[] = {
297         "arm,cortex-a7",
298         NULL,
299 };
300
301 static bool is_little_cpu(struct device_node *cn)
302 {
303         const char * const *lc;
304         for (lc = little_cores; *lc; lc++)
305                 if (of_device_is_compatible(cn, *lc))
306                         return true;
307         return false;
308 }
309
310 void __init arch_get_fast_and_slow_cpus(struct cpumask *fast,
311                                         struct cpumask *slow)
312 {
313         struct device_node *cn = NULL;
314         int cpu;
315
316         cpumask_clear(fast);
317         cpumask_clear(slow);
318
319         /*
320          * Use the config options if they are given. This helps testing
321          * HMP scheduling on systems without a big.LITTLE architecture.
322          */
323         if (strlen(CONFIG_HMP_FAST_CPU_MASK) && strlen(CONFIG_HMP_SLOW_CPU_MASK)) {
324                 if (cpulist_parse(CONFIG_HMP_FAST_CPU_MASK, fast))
325                         WARN(1, "Failed to parse HMP fast cpu mask!\n");
326                 if (cpulist_parse(CONFIG_HMP_SLOW_CPU_MASK, slow))
327                         WARN(1, "Failed to parse HMP slow cpu mask!\n");
328                 return;
329         }
330
331         /*
332          * Else, parse device tree for little cores.
333          */
334         while ((cn = of_find_node_by_type(cn, "cpu"))) {
335
336                 const u32 *mpidr;
337                 int len;
338
339                 mpidr = of_get_property(cn, "reg", &len);
340                 if (!mpidr || len != 4) {
341                         pr_err("* %s missing reg property\n", cn->full_name);
342                         continue;
343                 }
344
345                 cpu = get_logical_index(be32_to_cpup(mpidr));
346                 if (cpu == -EINVAL) {
347                         pr_err("couldn't get logical index for mpidr %x\n",
348                                                         be32_to_cpup(mpidr));
349                         break;
350                 }
351
352                 if (is_little_cpu(cn))
353                         cpumask_set_cpu(cpu, slow);
354                 else
355                         cpumask_set_cpu(cpu, fast);
356         }
357
358         if (!cpumask_empty(fast) && !cpumask_empty(slow))
359                 return;
360
361         /*
362          * We didn't find both big and little cores so let's call all cores
363          * fast as this will keep the system running, with all cores being
364          * treated equal.
365          */
366         cpumask_setall(fast);
367         cpumask_clear(slow);
368 }
369
370 struct cpumask hmp_slow_cpu_mask;
371
372 void __init arch_get_hmp_domains(struct list_head *hmp_domains_list)
373 {
374         struct cpumask hmp_fast_cpu_mask;
375         struct hmp_domain *domain;
376
377         arch_get_fast_and_slow_cpus(&hmp_fast_cpu_mask, &hmp_slow_cpu_mask);
378
379         /*
380          * Initialize hmp_domains
381          * Must be ordered with respect to compute capacity.
382          * Fastest domain at head of list.
383          */
384         if(!cpumask_empty(&hmp_slow_cpu_mask)) {
385                 domain = (struct hmp_domain *)
386                         kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
387                 cpumask_copy(&domain->possible_cpus, &hmp_slow_cpu_mask);
388                 cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
389                 list_add(&domain->hmp_domains, hmp_domains_list);
390         }
391         domain = (struct hmp_domain *)
392                 kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
393         cpumask_copy(&domain->possible_cpus, &hmp_fast_cpu_mask);
394         cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
395         list_add(&domain->hmp_domains, hmp_domains_list);
396 }
397 #endif /* CONFIG_SCHED_HMP */
398
399
400 /*
401  * cluster_to_logical_mask - return cpu logical mask of CPUs in a cluster
402  * @socket_id:          cluster HW identifier
403  * @cluster_mask:       the cpumask location to be initialized, modified by the
404  *                      function only if return value == 0
405  *
406  * Return:
407  *
408  * 0 on success
409  * -EINVAL if cluster_mask is NULL or there is no record matching socket_id
410  */
411 int cluster_to_logical_mask(unsigned int socket_id, cpumask_t *cluster_mask)
412 {
413         int cpu;
414
415         if (!cluster_mask)
416                 return -EINVAL;
417
418         for_each_online_cpu(cpu)
419                 if (socket_id == topology_physical_package_id(cpu)) {
420                         cpumask_copy(cluster_mask, topology_core_cpumask(cpu));
421                         return 0;
422                 }
423
424         return -EINVAL;
425 }
426
427 /*
428  * init_cpu_topology is called at boot when only one cpu is running
429  * which prevent simultaneous write access to cpu_topology array
430  */
431 void __init init_cpu_topology(void)
432 {
433         unsigned int cpu;
434
435         /* init core mask and power*/
436         for_each_possible_cpu(cpu) {
437                 struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
438
439                 cpu_topo->thread_id = -1;
440                 cpu_topo->core_id =  -1;
441                 cpu_topo->socket_id = -1;
442                 cpumask_clear(&cpu_topo->core_sibling);
443                 cpumask_clear(&cpu_topo->thread_sibling);
444
445                 set_power_scale(cpu, SCHED_POWER_SCALE);
446         }
447         smp_wmb();
448
449         parse_dt_topology();
450 }