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