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