1 /*
2 * linux/arch/arm/kernel/smp.c
3 *
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/cache.h>
17 #include <linux/profile.h>
18 #include <linux/errno.h>
19 #include <linux/mm.h>
20 #include <linux/err.h>
21 #include <linux/cpu.h>
22 #include <linux/seq_file.h>
23 #include <linux/irq.h>
24 #include <linux/percpu.h>
25 #include <linux/clockchips.h>
26 #include <linux/completion.h>
27 #include <linux/cpufreq.h>
28 #include <linux/irq_work.h>
29
30 #include <linux/atomic.h>
31 #include <asm/smp.h>
32 #include <asm/cacheflush.h>
33 #include <asm/cpu.h>
34 #include <asm/cputype.h>
35 #include <asm/exception.h>
36 #include <asm/idmap.h>
37 #include <asm/topology.h>
38 #include <asm/mmu_context.h>
39 #include <asm/pgtable.h>
40 #include <asm/pgalloc.h>
41 #include <asm/processor.h>
42 #include <asm/sections.h>
43 #include <asm/tlbflush.h>
44 #include <asm/ptrace.h>
45 #include <asm/smp_plat.h>
46 #include <asm/virt.h>
47 #include <asm/mach/arch.h>
48 #include <asm/mpu.h>
49
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/ipi.h>
52
53 /*
54 * as from 2.5, kernels no longer have an init_tasks structure
55 * so we need some other way of telling a new secondary core
56 * where to place its SVC stack
57 */
58 struct secondary_data secondary_data;
59
60 /*
61 * control for which core is the next to come out of the secondary
62 * boot "holding pen"
63 */
64 volatile int pen_release = -1;
65
66 enum ipi_msg_type {
67 IPI_WAKEUP,
68 IPI_TIMER,
69 IPI_RESCHEDULE,
70 IPI_CALL_FUNC,
71 IPI_CALL_FUNC_SINGLE,
72 IPI_CPU_STOP,
73 IPI_IRQ_WORK,
74 IPI_COMPLETION,
75 IPI_CPU_BACKTRACE,
76 };
77
78 static DECLARE_COMPLETION(cpu_running);
79
80 static struct smp_operations smp_ops;
81
smp_set_ops(struct smp_operations * ops)82 void __init smp_set_ops(struct smp_operations *ops)
83 {
84 if (ops)
85 smp_ops = *ops;
86 };
87
get_arch_pgd(pgd_t * pgd)88 static unsigned long get_arch_pgd(pgd_t *pgd)
89 {
90 #ifdef CONFIG_ARM_LPAE
91 return __phys_to_pfn(virt_to_phys(pgd));
92 #else
93 return virt_to_phys(pgd);
94 #endif
95 }
96
__cpu_up(unsigned int cpu,struct task_struct * idle)97 int __cpu_up(unsigned int cpu, struct task_struct *idle)
98 {
99 int ret;
100
101 if (!smp_ops.smp_boot_secondary)
102 return -ENOSYS;
103
104 /*
105 * We need to tell the secondary core where to find
106 * its stack and the page tables.
107 */
108 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
109 #ifdef CONFIG_ARM_MPU
110 secondary_data.mpu_rgn_szr = mpu_rgn_info.rgns[MPU_RAM_REGION].drsr;
111 #endif
112
113 #ifdef CONFIG_MMU
114 secondary_data.pgdir = virt_to_phys(idmap_pgd);
115 secondary_data.swapper_pg_dir = get_arch_pgd(swapper_pg_dir);
116 #endif
117 sync_cache_w(&secondary_data);
118
119 /*
120 * Now bring the CPU into our world.
121 */
122 ret = smp_ops.smp_boot_secondary(cpu, idle);
123 if (ret == 0) {
124 /*
125 * CPU was successfully started, wait for it
126 * to come online or time out.
127 */
128 wait_for_completion_timeout(&cpu_running,
129 msecs_to_jiffies(1000));
130
131 if (!cpu_online(cpu)) {
132 pr_crit("CPU%u: failed to come online\n", cpu);
133 ret = -EIO;
134 }
135 } else {
136 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
137 }
138
139
140 memset(&secondary_data, 0, sizeof(secondary_data));
141 return ret;
142 }
143
144 /* platform specific SMP operations */
smp_init_cpus(void)145 void __init smp_init_cpus(void)
146 {
147 if (smp_ops.smp_init_cpus)
148 smp_ops.smp_init_cpus();
149 }
150
platform_can_cpu_hotplug(void)151 int platform_can_cpu_hotplug(void)
152 {
153 #ifdef CONFIG_HOTPLUG_CPU
154 if (smp_ops.cpu_kill)
155 return 1;
156 #endif
157
158 return 0;
159 }
160
161 #ifdef CONFIG_HOTPLUG_CPU
platform_cpu_kill(unsigned int cpu)162 static int platform_cpu_kill(unsigned int cpu)
163 {
164 if (smp_ops.cpu_kill)
165 return smp_ops.cpu_kill(cpu);
166 return 1;
167 }
168
platform_cpu_disable(unsigned int cpu)169 static int platform_cpu_disable(unsigned int cpu)
170 {
171 if (smp_ops.cpu_disable)
172 return smp_ops.cpu_disable(cpu);
173
174 /*
175 * By default, allow disabling all CPUs except the first one,
176 * since this is special on a lot of platforms, e.g. because
177 * of clock tick interrupts.
178 */
179 return cpu == 0 ? -EPERM : 0;
180 }
181 /*
182 * __cpu_disable runs on the processor to be shutdown.
183 */
__cpu_disable(void)184 int __cpu_disable(void)
185 {
186 unsigned int cpu = smp_processor_id();
187 int ret;
188
189 ret = platform_cpu_disable(cpu);
190 if (ret)
191 return ret;
192
193 /*
194 * Take this CPU offline. Once we clear this, we can't return,
195 * and we must not schedule until we're ready to give up the cpu.
196 */
197 set_cpu_online(cpu, false);
198
199 /*
200 * OK - migrate IRQs away from this CPU
201 */
202 migrate_irqs();
203
204 /*
205 * Flush user cache and TLB mappings, and then remove this CPU
206 * from the vm mask set of all processes.
207 *
208 * Caches are flushed to the Level of Unification Inner Shareable
209 * to write-back dirty lines to unified caches shared by all CPUs.
210 */
211 flush_cache_louis();
212 local_flush_tlb_all();
213
214 clear_tasks_mm_cpumask(cpu);
215
216 return 0;
217 }
218
219 static DECLARE_COMPLETION(cpu_died);
220
221 /*
222 * called on the thread which is asking for a CPU to be shutdown -
223 * waits until shutdown has completed, or it is timed out.
224 */
__cpu_die(unsigned int cpu)225 void __cpu_die(unsigned int cpu)
226 {
227 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
228 pr_err("CPU%u: cpu didn't die\n", cpu);
229 return;
230 }
231 printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
232
233 /*
234 * platform_cpu_kill() is generally expected to do the powering off
235 * and/or cutting of clocks to the dying CPU. Optionally, this may
236 * be done by the CPU which is dying in preference to supporting
237 * this call, but that means there is _no_ synchronisation between
238 * the requesting CPU and the dying CPU actually losing power.
239 */
240 if (!platform_cpu_kill(cpu))
241 printk("CPU%u: unable to kill\n", cpu);
242 }
243
244 /*
245 * Called from the idle thread for the CPU which has been shutdown.
246 *
247 * Note that we disable IRQs here, but do not re-enable them
248 * before returning to the caller. This is also the behaviour
249 * of the other hotplug-cpu capable cores, so presumably coming
250 * out of idle fixes this.
251 */
cpu_die(void)252 void __ref cpu_die(void)
253 {
254 unsigned int cpu = smp_processor_id();
255
256 idle_task_exit();
257
258 local_irq_disable();
259
260 /*
261 * Flush the data out of the L1 cache for this CPU. This must be
262 * before the completion to ensure that data is safely written out
263 * before platform_cpu_kill() gets called - which may disable
264 * *this* CPU and power down its cache.
265 */
266 flush_cache_louis();
267
268 /*
269 * Tell __cpu_die() that this CPU is now safe to dispose of. Once
270 * this returns, power and/or clocks can be removed at any point
271 * from this CPU and its cache by platform_cpu_kill().
272 */
273 complete(&cpu_died);
274
275 /*
276 * Ensure that the cache lines associated with that completion are
277 * written out. This covers the case where _this_ CPU is doing the
278 * powering down, to ensure that the completion is visible to the
279 * CPU waiting for this one.
280 */
281 flush_cache_louis();
282
283 /*
284 * The actual CPU shutdown procedure is at least platform (if not
285 * CPU) specific. This may remove power, or it may simply spin.
286 *
287 * Platforms are generally expected *NOT* to return from this call,
288 * although there are some which do because they have no way to
289 * power down the CPU. These platforms are the _only_ reason we
290 * have a return path which uses the fragment of assembly below.
291 *
292 * The return path should not be used for platforms which can
293 * power off the CPU.
294 */
295 if (smp_ops.cpu_die)
296 smp_ops.cpu_die(cpu);
297
298 pr_warn("CPU%u: smp_ops.cpu_die() returned, trying to resuscitate\n",
299 cpu);
300
301 /*
302 * Do not return to the idle loop - jump back to the secondary
303 * cpu initialisation. There's some initialisation which needs
304 * to be repeated to undo the effects of taking the CPU offline.
305 */
306 __asm__("mov sp, %0\n"
307 " mov fp, #0\n"
308 " b secondary_start_kernel"
309 :
310 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
311 }
312 #endif /* CONFIG_HOTPLUG_CPU */
313
314 /*
315 * Called by both boot and secondaries to move global data into
316 * per-processor storage.
317 */
smp_store_cpu_info(unsigned int cpuid)318 static void smp_store_cpu_info(unsigned int cpuid)
319 {
320 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
321
322 cpu_info->loops_per_jiffy = loops_per_jiffy;
323 cpu_info->cpuid = read_cpuid_id();
324
325 store_cpu_topology(cpuid);
326 }
327
328 /*
329 * This is the secondary CPU boot entry. We're using this CPUs
330 * idle thread stack, but a set of temporary page tables.
331 */
secondary_start_kernel(void)332 asmlinkage void secondary_start_kernel(void)
333 {
334 struct mm_struct *mm = &init_mm;
335 unsigned int cpu;
336
337 /*
338 * The identity mapping is uncached (strongly ordered), so
339 * switch away from it before attempting any exclusive accesses.
340 */
341 cpu_switch_mm(mm->pgd, mm);
342 local_flush_bp_all();
343 enter_lazy_tlb(mm, current);
344 local_flush_tlb_all();
345
346 /*
347 * All kernel threads share the same mm context; grab a
348 * reference and switch to it.
349 */
350 cpu = smp_processor_id();
351 atomic_inc(&mm->mm_count);
352 current->active_mm = mm;
353 cpumask_set_cpu(cpu, mm_cpumask(mm));
354
355 cpu_init();
356
357 printk("CPU%u: Booted secondary processor\n", cpu);
358
359 preempt_disable();
360 trace_hardirqs_off();
361
362 /*
363 * Give the platform a chance to do its own initialisation.
364 */
365 if (smp_ops.smp_secondary_init)
366 smp_ops.smp_secondary_init(cpu);
367
368 notify_cpu_starting(cpu);
369
370 calibrate_delay();
371
372 smp_store_cpu_info(cpu);
373
374 /*
375 * OK, now it's safe to let the boot CPU continue. Wait for
376 * the CPU migration code to notice that the CPU is online
377 * before we continue - which happens after __cpu_up returns.
378 */
379 set_cpu_online(cpu, true);
380 complete(&cpu_running);
381
382 local_irq_enable();
383 local_fiq_enable();
384
385 /*
386 * OK, it's off to the idle thread for us
387 */
388 cpu_startup_entry(CPUHP_ONLINE);
389 }
390
smp_cpus_done(unsigned int max_cpus)391 void __init smp_cpus_done(unsigned int max_cpus)
392 {
393 int cpu;
394 unsigned long bogosum = 0;
395
396 for_each_online_cpu(cpu)
397 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
398
399 printk(KERN_INFO "SMP: Total of %d processors activated "
400 "(%lu.%02lu BogoMIPS).\n",
401 num_online_cpus(),
402 bogosum / (500000/HZ),
403 (bogosum / (5000/HZ)) % 100);
404
405 hyp_mode_check();
406 }
407
smp_prepare_boot_cpu(void)408 void __init smp_prepare_boot_cpu(void)
409 {
410 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
411 }
412
smp_prepare_cpus(unsigned int max_cpus)413 void __init smp_prepare_cpus(unsigned int max_cpus)
414 {
415 unsigned int ncores = num_possible_cpus();
416
417 init_cpu_topology();
418
419 smp_store_cpu_info(smp_processor_id());
420
421 /*
422 * are we trying to boot more cores than exist?
423 */
424 if (max_cpus > ncores)
425 max_cpus = ncores;
426 if (ncores > 1 && max_cpus) {
427 /*
428 * Initialise the present map, which describes the set of CPUs
429 * actually populated at the present time. A platform should
430 * re-initialize the map in the platforms smp_prepare_cpus()
431 * if present != possible (e.g. physical hotplug).
432 */
433 init_cpu_present(cpu_possible_mask);
434
435 /*
436 * Initialise the SCU if there are more than one CPU
437 * and let them know where to start.
438 */
439 if (smp_ops.smp_prepare_cpus)
440 smp_ops.smp_prepare_cpus(max_cpus);
441 }
442 }
443
444 static void (*__smp_cross_call)(const struct cpumask *, unsigned int);
445
set_smp_cross_call(void (* fn)(const struct cpumask *,unsigned int))446 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
447 {
448 if (!__smp_cross_call)
449 __smp_cross_call = fn;
450 }
451
452 static const char *ipi_types[NR_IPI] __tracepoint_string = {
453 #define S(x,s) [x] = s
454 S(IPI_WAKEUP, "CPU wakeup interrupts"),
455 S(IPI_TIMER, "Timer broadcast interrupts"),
456 S(IPI_RESCHEDULE, "Rescheduling interrupts"),
457 S(IPI_CALL_FUNC, "Function call interrupts"),
458 S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
459 S(IPI_CPU_STOP, "CPU stop interrupts"),
460 S(IPI_IRQ_WORK, "IRQ work interrupts"),
461 S(IPI_COMPLETION, "completion interrupts"),
462 S(IPI_CPU_BACKTRACE, "CPU backtrace"),
463 };
464
smp_cross_call(const struct cpumask * target,unsigned int ipinr)465 static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
466 {
467 trace_ipi_raise(target, ipi_types[ipinr]);
468 __smp_cross_call(target, ipinr);
469 }
470
show_ipi_list(struct seq_file * p,int prec)471 void show_ipi_list(struct seq_file *p, int prec)
472 {
473 unsigned int cpu, i;
474
475 for (i = 0; i < NR_IPI; i++) {
476 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
477
478 for_each_online_cpu(cpu)
479 seq_printf(p, "%10u ",
480 __get_irq_stat(cpu, ipi_irqs[i]));
481
482 seq_printf(p, " %s\n", ipi_types[i]);
483 }
484 }
485
smp_irq_stat_cpu(unsigned int cpu)486 u64 smp_irq_stat_cpu(unsigned int cpu)
487 {
488 u64 sum = 0;
489 int i;
490
491 for (i = 0; i < NR_IPI; i++)
492 sum += __get_irq_stat(cpu, ipi_irqs[i]);
493
494 return sum;
495 }
496
arch_send_call_function_ipi_mask(const struct cpumask * mask)497 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
498 {
499 smp_cross_call(mask, IPI_CALL_FUNC);
500 }
501
arch_send_wakeup_ipi_mask(const struct cpumask * mask)502 void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
503 {
504 smp_cross_call(mask, IPI_WAKEUP);
505 }
506
arch_send_call_function_single_ipi(int cpu)507 void arch_send_call_function_single_ipi(int cpu)
508 {
509 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
510 }
511
512 #ifdef CONFIG_IRQ_WORK
arch_irq_work_raise(void)513 void arch_irq_work_raise(void)
514 {
515 if (arch_irq_work_has_interrupt())
516 smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
517 }
518 #endif
519
520 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
tick_broadcast(const struct cpumask * mask)521 void tick_broadcast(const struct cpumask *mask)
522 {
523 smp_cross_call(mask, IPI_TIMER);
524 }
525 #endif
526
527 static DEFINE_RAW_SPINLOCK(stop_lock);
528
529 /*
530 * ipi_cpu_stop - handle IPI from smp_send_stop()
531 */
ipi_cpu_stop(unsigned int cpu)532 static void ipi_cpu_stop(unsigned int cpu)
533 {
534 if (system_state == SYSTEM_BOOTING ||
535 system_state == SYSTEM_RUNNING) {
536 raw_spin_lock(&stop_lock);
537 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
538 dump_stack();
539 raw_spin_unlock(&stop_lock);
540 }
541
542 set_cpu_online(cpu, false);
543
544 local_fiq_disable();
545 local_irq_disable();
546
547 while (1)
548 cpu_relax();
549 }
550
551 static DEFINE_PER_CPU(struct completion *, cpu_completion);
552
register_ipi_completion(struct completion * completion,int cpu)553 int register_ipi_completion(struct completion *completion, int cpu)
554 {
555 per_cpu(cpu_completion, cpu) = completion;
556 return IPI_COMPLETION;
557 }
558
ipi_complete(unsigned int cpu)559 static void ipi_complete(unsigned int cpu)
560 {
561 complete(per_cpu(cpu_completion, cpu));
562 }
563
564 static cpumask_t backtrace_mask;
565 static DEFINE_RAW_SPINLOCK(backtrace_lock);
566
567 /* "in progress" flag of arch_trigger_all_cpu_backtrace */
568 static unsigned long backtrace_flag;
569
smp_send_all_cpu_backtrace(void)570 void smp_send_all_cpu_backtrace(void)
571 {
572 unsigned int this_cpu = smp_processor_id();
573 int i;
574
575 if (test_and_set_bit(0, &backtrace_flag))
576 /*
577 * If there is already a trigger_all_cpu_backtrace() in progress
578 * (backtrace_flag == 1), don't output double cpu dump infos.
579 */
580 return;
581
582 cpumask_copy(&backtrace_mask, cpu_online_mask);
583 cpu_clear(this_cpu, backtrace_mask);
584
585 pr_info("Backtrace for cpu %d (current):\n", this_cpu);
586 dump_stack();
587
588 pr_info("\nsending IPI to all other CPUs:\n");
589 smp_cross_call(&backtrace_mask, IPI_CPU_BACKTRACE);
590
591 /* Wait for up to 10 seconds for all other CPUs to do the backtrace */
592 for (i = 0; i < 10 * 1000; i++) {
593 if (cpumask_empty(&backtrace_mask))
594 break;
595 mdelay(1);
596 }
597
598 clear_bit(0, &backtrace_flag);
599 smp_mb__after_atomic();
600 }
601
602 /*
603 * ipi_cpu_backtrace - handle IPI from smp_send_all_cpu_backtrace()
604 */
ipi_cpu_backtrace(unsigned int cpu,struct pt_regs * regs)605 static void ipi_cpu_backtrace(unsigned int cpu, struct pt_regs *regs)
606 {
607 if (cpu_isset(cpu, backtrace_mask)) {
608 raw_spin_lock(&backtrace_lock);
609 pr_warning("IPI backtrace for cpu %d\n", cpu);
610 show_regs(regs);
611 raw_spin_unlock(&backtrace_lock);
612 cpu_clear(cpu, backtrace_mask);
613 }
614 }
615
616 /*
617 * Main handler for inter-processor interrupts
618 */
do_IPI(int ipinr,struct pt_regs * regs)619 asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
620 {
621 handle_IPI(ipinr, regs);
622 }
623
handle_IPI(int ipinr,struct pt_regs * regs)624 void handle_IPI(int ipinr, struct pt_regs *regs)
625 {
626 unsigned int cpu = smp_processor_id();
627 struct pt_regs *old_regs = set_irq_regs(regs);
628
629 if ((unsigned)ipinr < NR_IPI) {
630 trace_ipi_entry(ipi_types[ipinr]);
631 __inc_irq_stat(cpu, ipi_irqs[ipinr]);
632 }
633
634 switch (ipinr) {
635 case IPI_WAKEUP:
636 break;
637
638 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
639 case IPI_TIMER:
640 irq_enter();
641 tick_receive_broadcast();
642 irq_exit();
643 break;
644 #endif
645
646 case IPI_RESCHEDULE:
647 scheduler_ipi();
648 break;
649
650 case IPI_CALL_FUNC:
651 irq_enter();
652 generic_smp_call_function_interrupt();
653 irq_exit();
654 break;
655
656 case IPI_CALL_FUNC_SINGLE:
657 irq_enter();
658 generic_smp_call_function_single_interrupt();
659 irq_exit();
660 break;
661
662 case IPI_CPU_STOP:
663 irq_enter();
664 ipi_cpu_stop(cpu);
665 irq_exit();
666 break;
667
668 #ifdef CONFIG_IRQ_WORK
669 case IPI_IRQ_WORK:
670 irq_enter();
671 irq_work_run();
672 irq_exit();
673 break;
674 #endif
675
676 case IPI_COMPLETION:
677 irq_enter();
678 ipi_complete(cpu);
679 irq_exit();
680 break;
681
682 case IPI_CPU_BACKTRACE:
683 ipi_cpu_backtrace(cpu, regs);
684 break;
685
686 default:
687 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
688 cpu, ipinr);
689 break;
690 }
691
692 if ((unsigned)ipinr < NR_IPI)
693 trace_ipi_exit(ipi_types[ipinr]);
694 set_irq_regs(old_regs);
695 }
696
smp_send_reschedule(int cpu)697 void smp_send_reschedule(int cpu)
698 {
699 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
700 }
701
smp_send_stop(void)702 void smp_send_stop(void)
703 {
704 unsigned long timeout;
705 struct cpumask mask;
706
707 cpumask_copy(&mask, cpu_online_mask);
708 cpumask_clear_cpu(smp_processor_id(), &mask);
709 if (!cpumask_empty(&mask))
710 smp_cross_call(&mask, IPI_CPU_STOP);
711
712 /* Wait up to one second for other CPUs to stop */
713 timeout = USEC_PER_SEC;
714 while (num_online_cpus() > 1 && timeout--)
715 udelay(1);
716
717 if (num_online_cpus() > 1)
718 pr_warn("SMP: failed to stop secondary CPUs\n");
719 }
720
721 /*
722 * not supported here
723 */
setup_profiling_timer(unsigned int multiplier)724 int setup_profiling_timer(unsigned int multiplier)
725 {
726 return -EINVAL;
727 }
728
729 #ifdef CONFIG_CPU_FREQ
730
731 static DEFINE_PER_CPU(unsigned long, l_p_j_ref);
732 static DEFINE_PER_CPU(unsigned long, l_p_j_ref_freq);
733 static unsigned long global_l_p_j_ref;
734 static unsigned long global_l_p_j_ref_freq;
735
cpufreq_callback(struct notifier_block * nb,unsigned long val,void * data)736 static int cpufreq_callback(struct notifier_block *nb,
737 unsigned long val, void *data)
738 {
739 struct cpufreq_freqs *freq = data;
740 int cpu = freq->cpu;
741
742 if (freq->flags & CPUFREQ_CONST_LOOPS)
743 return NOTIFY_OK;
744
745 if (!per_cpu(l_p_j_ref, cpu)) {
746 per_cpu(l_p_j_ref, cpu) =
747 per_cpu(cpu_data, cpu).loops_per_jiffy;
748 per_cpu(l_p_j_ref_freq, cpu) = freq->old;
749 if (!global_l_p_j_ref) {
750 global_l_p_j_ref = loops_per_jiffy;
751 global_l_p_j_ref_freq = freq->old;
752 }
753 }
754
755 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
756 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
757 loops_per_jiffy = cpufreq_scale(global_l_p_j_ref,
758 global_l_p_j_ref_freq,
759 freq->new);
760 per_cpu(cpu_data, cpu).loops_per_jiffy =
761 cpufreq_scale(per_cpu(l_p_j_ref, cpu),
762 per_cpu(l_p_j_ref_freq, cpu),
763 freq->new);
764 }
765 return NOTIFY_OK;
766 }
767
768 static struct notifier_block cpufreq_notifier = {
769 .notifier_call = cpufreq_callback,
770 };
771
register_cpufreq_notifier(void)772 static int __init register_cpufreq_notifier(void)
773 {
774 return cpufreq_register_notifier(&cpufreq_notifier,
775 CPUFREQ_TRANSITION_NOTIFIER);
776 }
777 core_initcall(register_cpufreq_notifier);
778
779 #endif
780