ARM: dtsi: rk3228: add psci support
[firefly-linux-kernel-4.4.55.git] / kernel / smp.c
1 /*
2  * Generic helpers for smp ipi calls
3  *
4  * (C) Jens Axboe <jens.axboe@oracle.com> 2008
5  */
6 #include <linux/rcupdate.h>
7 #include <linux/rculist.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/percpu.h>
11 #include <linux/init.h>
12 #include <linux/gfp.h>
13 #include <linux/smp.h>
14 #include <linux/cpu.h>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/smp.h>
17
18 #include "smpboot.h"
19
20 #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
21 enum {
22         CSD_FLAG_LOCK           = 0x01,
23 };
24
25 struct call_function_data {
26         struct call_single_data __percpu *csd;
27         cpumask_var_t           cpumask;
28         cpumask_var_t           cpumask_ipi;
29 };
30
31 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
32
33 struct call_single_queue {
34         struct list_head        list;
35         raw_spinlock_t          lock;
36 };
37
38 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
39
40 static int
41 hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
42 {
43         long cpu = (long)hcpu;
44         struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
45
46         switch (action) {
47         case CPU_UP_PREPARE:
48         case CPU_UP_PREPARE_FROZEN:
49                 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
50                                 cpu_to_node(cpu)))
51                         return notifier_from_errno(-ENOMEM);
52                 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
53                                 cpu_to_node(cpu)))
54                         return notifier_from_errno(-ENOMEM);
55                 cfd->csd = alloc_percpu(struct call_single_data);
56                 if (!cfd->csd) {
57                         free_cpumask_var(cfd->cpumask);
58                         return notifier_from_errno(-ENOMEM);
59                 }
60                 break;
61
62 #ifdef CONFIG_HOTPLUG_CPU
63         case CPU_UP_CANCELED:
64         case CPU_UP_CANCELED_FROZEN:
65
66         case CPU_DEAD:
67         case CPU_DEAD_FROZEN:
68                 free_cpumask_var(cfd->cpumask);
69                 free_cpumask_var(cfd->cpumask_ipi);
70                 free_percpu(cfd->csd);
71                 break;
72 #endif
73         };
74
75         return NOTIFY_OK;
76 }
77
78 static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
79         .notifier_call          = hotplug_cfd,
80 };
81
82 void __init call_function_init(void)
83 {
84         void *cpu = (void *)(long)smp_processor_id();
85         int i;
86
87         for_each_possible_cpu(i) {
88                 struct call_single_queue *q = &per_cpu(call_single_queue, i);
89
90                 raw_spin_lock_init(&q->lock);
91                 INIT_LIST_HEAD(&q->list);
92         }
93
94         hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
95         register_cpu_notifier(&hotplug_cfd_notifier);
96 }
97
98 /*
99  * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
100  *
101  * For non-synchronous ipi calls the csd can still be in use by the
102  * previous function call. For multi-cpu calls its even more interesting
103  * as we'll have to ensure no other cpu is observing our csd.
104  */
105 static void csd_lock_wait(struct call_single_data *csd)
106 {
107         while (csd->flags & CSD_FLAG_LOCK)
108                 cpu_relax();
109 }
110
111 static void csd_lock(struct call_single_data *csd)
112 {
113         csd_lock_wait(csd);
114         csd->flags |= CSD_FLAG_LOCK;
115
116         /*
117          * prevent CPU from reordering the above assignment
118          * to ->flags with any subsequent assignments to other
119          * fields of the specified call_single_data structure:
120          */
121         smp_mb();
122 }
123
124 static void csd_unlock(struct call_single_data *csd)
125 {
126         WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
127
128         /*
129          * ensure we're all done before releasing data:
130          */
131         smp_mb();
132
133         csd->flags &= ~CSD_FLAG_LOCK;
134 }
135
136 /*
137  * Insert a previously allocated call_single_data element
138  * for execution on the given CPU. data must already have
139  * ->func, ->info, and ->flags set.
140  */
141 static
142 void generic_exec_single(int cpu, struct call_single_data *csd, int wait)
143 {
144         struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
145         unsigned long flags;
146         int ipi;
147
148         raw_spin_lock_irqsave(&dst->lock, flags);
149         ipi = list_empty(&dst->list);
150         list_add_tail(&csd->list, &dst->list);
151         raw_spin_unlock_irqrestore(&dst->lock, flags);
152
153         /*
154          * The list addition should be visible before sending the IPI
155          * handler locks the list to pull the entry off it because of
156          * normal cache coherency rules implied by spinlocks.
157          *
158          * If IPIs can go out of order to the cache coherency protocol
159          * in an architecture, sufficient synchronisation should be added
160          * to arch code to make it appear to obey cache coherency WRT
161          * locking and barrier primitives. Generic code isn't really
162          * equipped to do the right thing...
163          */
164         if (ipi) {
165                 trace_smp_call_func_send(csd->func, cpu);
166                 arch_send_call_function_single_ipi(cpu);
167         }
168
169         if (wait)
170                 csd_lock_wait(csd);
171 }
172
173 /*
174  * Invoked by arch to handle an IPI for call function single. Must be
175  * called from the arch with interrupts disabled.
176  */
177 void generic_smp_call_function_single_interrupt(void)
178 {
179         struct call_single_queue *q = &__get_cpu_var(call_single_queue);
180         LIST_HEAD(list);
181
182         /*
183          * Shouldn't receive this interrupt on a cpu that is not yet online.
184          */
185         WARN_ON_ONCE(!cpu_online(smp_processor_id()));
186
187         raw_spin_lock(&q->lock);
188         list_replace_init(&q->list, &list);
189         raw_spin_unlock(&q->lock);
190
191         while (!list_empty(&list)) {
192                 struct call_single_data *csd;
193                 unsigned int csd_flags;
194
195                 csd = list_entry(list.next, struct call_single_data, list);
196                 list_del(&csd->list);
197
198                 /*
199                  * 'csd' can be invalid after this call if flags == 0
200                  * (when called through generic_exec_single()),
201                  * so save them away before making the call:
202                  */
203                 csd_flags = csd->flags;
204                 trace_smp_call_func_entry(csd->func);
205                 csd->func(csd->info);
206                 trace_smp_call_func_exit(csd->func);
207
208                 /*
209                  * Unlocked CSDs are valid through generic_exec_single():
210                  */
211                 if (csd_flags & CSD_FLAG_LOCK)
212                         csd_unlock(csd);
213         }
214 }
215
216 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
217
218 /*
219  * smp_call_function_single - Run a function on a specific CPU
220  * @func: The function to run. This must be fast and non-blocking.
221  * @info: An arbitrary pointer to pass to the function.
222  * @wait: If true, wait until function has completed on other CPUs.
223  *
224  * Returns 0 on success, else a negative status code.
225  */
226 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
227                              int wait)
228 {
229         struct call_single_data d = {
230                 .flags = 0,
231         };
232         unsigned long flags;
233         int this_cpu;
234         int err = 0;
235
236         trace_smp_call_func_send(func, cpu);
237         /*
238          * prevent preemption and reschedule on another processor,
239          * as well as CPU removal
240          */
241         this_cpu = get_cpu();
242
243         /*
244          * Can deadlock when called with interrupts disabled.
245          * We allow cpu's that are not yet online though, as no one else can
246          * send smp call function interrupt to this cpu and as such deadlocks
247          * can't happen.
248          */
249         WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
250                      && !oops_in_progress);
251
252         if (cpu == this_cpu) {
253                 local_irq_save(flags);
254                 trace_smp_call_func_entry(func);
255                 func(info);
256                 trace_smp_call_func_exit(func);
257                 local_irq_restore(flags);
258         } else {
259                 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
260                         struct call_single_data *csd = &d;
261
262                         if (!wait)
263                                 csd = &__get_cpu_var(csd_data);
264
265                         csd_lock(csd);
266
267                         csd->func = func;
268                         csd->info = info;
269                         generic_exec_single(cpu, csd, wait);
270                 } else {
271                         err = -ENXIO;   /* CPU not online */
272                 }
273         }
274
275         put_cpu();
276
277         return err;
278 }
279 EXPORT_SYMBOL(smp_call_function_single);
280
281 /*
282  * smp_call_function_any - Run a function on any of the given cpus
283  * @mask: The mask of cpus it can run on.
284  * @func: The function to run. This must be fast and non-blocking.
285  * @info: An arbitrary pointer to pass to the function.
286  * @wait: If true, wait until function has completed.
287  *
288  * Returns 0 on success, else a negative status code (if no cpus were online).
289  * Note that @wait will be implicitly turned on in case of allocation failures,
290  * since we fall back to on-stack allocation.
291  *
292  * Selection preference:
293  *      1) current cpu if in @mask
294  *      2) any cpu of current node if in @mask
295  *      3) any other online cpu in @mask
296  */
297 int smp_call_function_any(const struct cpumask *mask,
298                           smp_call_func_t func, void *info, int wait)
299 {
300         unsigned int cpu;
301         const struct cpumask *nodemask;
302         int ret;
303
304         /* Try for same CPU (cheapest) */
305         cpu = get_cpu();
306         if (cpumask_test_cpu(cpu, mask))
307                 goto call;
308
309         /* Try for same node. */
310         nodemask = cpumask_of_node(cpu_to_node(cpu));
311         for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
312              cpu = cpumask_next_and(cpu, nodemask, mask)) {
313                 if (cpu_online(cpu))
314                         goto call;
315         }
316
317         /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
318         cpu = cpumask_any_and(mask, cpu_online_mask);
319 call:
320         ret = smp_call_function_single(cpu, func, info, wait);
321         put_cpu();
322         return ret;
323 }
324 EXPORT_SYMBOL_GPL(smp_call_function_any);
325
326 /**
327  * __smp_call_function_single(): Run a function on a specific CPU
328  * @cpu: The CPU to run on.
329  * @data: Pre-allocated and setup data structure
330  * @wait: If true, wait until function has completed on specified CPU.
331  *
332  * Like smp_call_function_single(), but allow caller to pass in a
333  * pre-allocated data structure. Useful for embedding @data inside
334  * other structures, for instance.
335  */
336 void __smp_call_function_single(int cpu, struct call_single_data *csd,
337                                 int wait)
338 {
339         unsigned int this_cpu;
340         unsigned long flags;
341
342         this_cpu = get_cpu();
343         /*
344          * Can deadlock when called with interrupts disabled.
345          * We allow cpu's that are not yet online though, as no one else can
346          * send smp call function interrupt to this cpu and as such deadlocks
347          * can't happen.
348          */
349         WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
350                      && !oops_in_progress);
351
352         if (cpu == this_cpu) {
353                 local_irq_save(flags);
354                 csd->func(csd->info);
355                 local_irq_restore(flags);
356         } else {
357                 csd_lock(csd);
358                 generic_exec_single(cpu, csd, wait);
359         }
360         put_cpu();
361 }
362
363 /**
364  * smp_call_function_many(): Run a function on a set of other CPUs.
365  * @mask: The set of cpus to run on (only runs on online subset).
366  * @func: The function to run. This must be fast and non-blocking.
367  * @info: An arbitrary pointer to pass to the function.
368  * @wait: If true, wait (atomically) until function has completed
369  *        on other CPUs.
370  *
371  * If @wait is true, then returns once @func has returned.
372  *
373  * You must not call this function with disabled interrupts or from a
374  * hardware interrupt handler or from a bottom half handler. Preemption
375  * must be disabled when calling this function.
376  */
377 void smp_call_function_many(const struct cpumask *mask,
378                             smp_call_func_t func, void *info, bool wait)
379 {
380         struct call_function_data *cfd;
381         int cpu, next_cpu, this_cpu = smp_processor_id();
382
383         /*
384          * Can deadlock when called with interrupts disabled.
385          * We allow cpu's that are not yet online though, as no one else can
386          * send smp call function interrupt to this cpu and as such deadlocks
387          * can't happen.
388          */
389         WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
390                      && !oops_in_progress && !early_boot_irqs_disabled);
391
392         /* Try to fastpath.  So, what's a CPU they want? Ignoring this one. */
393         cpu = cpumask_first_and(mask, cpu_online_mask);
394         if (cpu == this_cpu)
395                 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
396
397         /* No online cpus?  We're done. */
398         if (cpu >= nr_cpu_ids)
399                 return;
400
401         /* Do we have another CPU which isn't us? */
402         next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
403         if (next_cpu == this_cpu)
404                 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
405
406         /* Fastpath: do that cpu by itself. */
407         if (next_cpu >= nr_cpu_ids) {
408                 smp_call_function_single(cpu, func, info, wait);
409                 return;
410         }
411
412         cfd = &__get_cpu_var(cfd_data);
413
414         cpumask_and(cfd->cpumask, mask, cpu_online_mask);
415         cpumask_clear_cpu(this_cpu, cfd->cpumask);
416
417         /* Some callers race with other cpus changing the passed mask */
418         if (unlikely(!cpumask_weight(cfd->cpumask)))
419                 return;
420
421         /*
422          * After we put an entry into the list, cfd->cpumask may be cleared
423          * again when another CPU sends another IPI for a SMP function call, so
424          * cfd->cpumask will be zero.
425          */
426         cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
427
428         for_each_cpu(cpu, cfd->cpumask) {
429                 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
430                 struct call_single_queue *dst =
431                                         &per_cpu(call_single_queue, cpu);
432                 unsigned long flags;
433
434                 csd_lock(csd);
435                 csd->func = func;
436                 csd->info = info;
437
438                 raw_spin_lock_irqsave(&dst->lock, flags);
439                 list_add_tail(&csd->list, &dst->list);
440                 raw_spin_unlock_irqrestore(&dst->lock, flags);
441         }
442
443         /* Send a message to all CPUs in the map */
444         arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
445
446         if (wait) {
447                 for_each_cpu(cpu, cfd->cpumask) {
448                         struct call_single_data *csd;
449
450                         csd = per_cpu_ptr(cfd->csd, cpu);
451                         csd_lock_wait(csd);
452                 }
453         }
454 }
455 EXPORT_SYMBOL(smp_call_function_many);
456
457 /**
458  * smp_call_function(): Run a function on all other CPUs.
459  * @func: The function to run. This must be fast and non-blocking.
460  * @info: An arbitrary pointer to pass to the function.
461  * @wait: If true, wait (atomically) until function has completed
462  *        on other CPUs.
463  *
464  * Returns 0.
465  *
466  * If @wait is true, then returns once @func has returned; otherwise
467  * it returns just before the target cpu calls @func.
468  *
469  * You must not call this function with disabled interrupts or from a
470  * hardware interrupt handler or from a bottom half handler.
471  */
472 int smp_call_function(smp_call_func_t func, void *info, int wait)
473 {
474         preempt_disable();
475         smp_call_function_many(cpu_online_mask, func, info, wait);
476         preempt_enable();
477
478         return 0;
479 }
480 EXPORT_SYMBOL(smp_call_function);
481 #endif /* USE_GENERIC_SMP_HELPERS */
482
483 /* Setup configured maximum number of CPUs to activate */
484 unsigned int setup_max_cpus = NR_CPUS;
485 EXPORT_SYMBOL(setup_max_cpus);
486
487
488 /*
489  * Setup routine for controlling SMP activation
490  *
491  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
492  * activation entirely (the MPS table probe still happens, though).
493  *
494  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
495  * greater than 0, limits the maximum number of CPUs activated in
496  * SMP mode to <NUM>.
497  */
498
499 void __weak arch_disable_smp_support(void) { }
500
501 static int __init nosmp(char *str)
502 {
503         setup_max_cpus = 0;
504         arch_disable_smp_support();
505
506         return 0;
507 }
508
509 early_param("nosmp", nosmp);
510
511 /* this is hard limit */
512 static int __init nrcpus(char *str)
513 {
514         int nr_cpus;
515
516         get_option(&str, &nr_cpus);
517         if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
518                 nr_cpu_ids = nr_cpus;
519
520         return 0;
521 }
522
523 early_param("nr_cpus", nrcpus);
524
525 static int __init maxcpus(char *str)
526 {
527         get_option(&str, &setup_max_cpus);
528         if (setup_max_cpus == 0)
529                 arch_disable_smp_support();
530
531         return 0;
532 }
533
534 early_param("maxcpus", maxcpus);
535
536 /* Setup number of possible processor ids */
537 int nr_cpu_ids __read_mostly = NR_CPUS;
538 EXPORT_SYMBOL(nr_cpu_ids);
539
540 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
541 void __init setup_nr_cpu_ids(void)
542 {
543         nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
544 }
545
546 /* Called by boot processor to activate the rest. */
547 void __init smp_init(void)
548 {
549         unsigned int cpu;
550
551         idle_threads_init();
552
553         /* FIXME: This should be done in userspace --RR */
554         for_each_present_cpu(cpu) {
555                 if (num_online_cpus() >= setup_max_cpus)
556                         break;
557                 if (!cpu_online(cpu))
558                         cpu_up(cpu);
559         }
560
561         /* Any cleanup work */
562         printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
563         smp_cpus_done(setup_max_cpus);
564 }
565
566 /*
567  * Call a function on all processors.  May be used during early boot while
568  * early_boot_irqs_disabled is set.  Use local_irq_save/restore() instead
569  * of local_irq_disable/enable().
570  */
571 int on_each_cpu(void (*func) (void *info), void *info, int wait)
572 {
573         unsigned long flags;
574         int ret = 0;
575
576         preempt_disable();
577         ret = smp_call_function(func, info, wait);
578         local_irq_save(flags);
579         func(info);
580         local_irq_restore(flags);
581         preempt_enable();
582         return ret;
583 }
584 EXPORT_SYMBOL(on_each_cpu);
585
586 /**
587  * on_each_cpu_mask(): Run a function on processors specified by
588  * cpumask, which may include the local processor.
589  * @mask: The set of cpus to run on (only runs on online subset).
590  * @func: The function to run. This must be fast and non-blocking.
591  * @info: An arbitrary pointer to pass to the function.
592  * @wait: If true, wait (atomically) until function has completed
593  *        on other CPUs.
594  *
595  * If @wait is true, then returns once @func has returned.
596  *
597  * You must not call this function with disabled interrupts or
598  * from a hardware interrupt handler or from a bottom half handler.
599  */
600 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
601                         void *info, bool wait)
602 {
603         int cpu = get_cpu();
604
605         smp_call_function_many(mask, func, info, wait);
606         if (cpumask_test_cpu(cpu, mask)) {
607                 local_irq_disable();
608                 func(info);
609                 local_irq_enable();
610         }
611         put_cpu();
612 }
613 EXPORT_SYMBOL(on_each_cpu_mask);
614
615 /*
616  * on_each_cpu_cond(): Call a function on each processor for which
617  * the supplied function cond_func returns true, optionally waiting
618  * for all the required CPUs to finish. This may include the local
619  * processor.
620  * @cond_func:  A callback function that is passed a cpu id and
621  *              the the info parameter. The function is called
622  *              with preemption disabled. The function should
623  *              return a blooean value indicating whether to IPI
624  *              the specified CPU.
625  * @func:       The function to run on all applicable CPUs.
626  *              This must be fast and non-blocking.
627  * @info:       An arbitrary pointer to pass to both functions.
628  * @wait:       If true, wait (atomically) until function has
629  *              completed on other CPUs.
630  * @gfp_flags:  GFP flags to use when allocating the cpumask
631  *              used internally by the function.
632  *
633  * The function might sleep if the GFP flags indicates a non
634  * atomic allocation is allowed.
635  *
636  * Preemption is disabled to protect against CPUs going offline but not online.
637  * CPUs going online during the call will not be seen or sent an IPI.
638  *
639  * You must not call this function with disabled interrupts or
640  * from a hardware interrupt handler or from a bottom half handler.
641  */
642 void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
643                         smp_call_func_t func, void *info, bool wait,
644                         gfp_t gfp_flags)
645 {
646         cpumask_var_t cpus;
647         int cpu, ret;
648
649         might_sleep_if(gfp_flags & __GFP_WAIT);
650
651         if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
652                 preempt_disable();
653                 for_each_online_cpu(cpu)
654                         if (cond_func(cpu, info))
655                                 cpumask_set_cpu(cpu, cpus);
656                 on_each_cpu_mask(cpus, func, info, wait);
657                 preempt_enable();
658                 free_cpumask_var(cpus);
659         } else {
660                 /*
661                  * No free cpumask, bother. No matter, we'll
662                  * just have to IPI them one by one.
663                  */
664                 preempt_disable();
665                 for_each_online_cpu(cpu)
666                         if (cond_func(cpu, info)) {
667                                 ret = smp_call_function_single(cpu, func,
668                                                                 info, wait);
669                                 WARN_ON_ONCE(ret);
670                         }
671                 preempt_enable();
672         }
673 }
674 EXPORT_SYMBOL(on_each_cpu_cond);
675
676 static void do_nothing(void *unused)
677 {
678 }
679
680 /**
681  * kick_all_cpus_sync - Force all cpus out of idle
682  *
683  * Used to synchronize the update of pm_idle function pointer. It's
684  * called after the pointer is updated and returns after the dummy
685  * callback function has been executed on all cpus. The execution of
686  * the function can only happen on the remote cpus after they have
687  * left the idle function which had been called via pm_idle function
688  * pointer. So it's guaranteed that nothing uses the previous pointer
689  * anymore.
690  */
691 void kick_all_cpus_sync(void)
692 {
693         /* Make sure the change is visible before we kick the cpus */
694         smp_mb();
695         smp_call_function(do_nothing, NULL, 1);
696 }
697 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);