CHROMIUM: chromeos_acpi: Enable USB wake from S3
[cascardo/linux.git] / kernel / cpu.c
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.h>
8 #include <linux/init.h>
9 #include <linux/notifier.h>
10 #include <linux/sched.h>
11 #include <linux/unistd.h>
12 #include <linux/cpu.h>
13 #include <linux/export.h>
14 #include <linux/kthread.h>
15 #include <linux/stop_machine.h>
16 #include <linux/mutex.h>
17 #include <linux/gfp.h>
18 #include <linux/suspend.h>
19
20 static ATOMIC_NOTIFIER_HEAD(idle_notifier);
21
22 void idle_notifier_register(struct notifier_block *n)
23 {
24         atomic_notifier_chain_register(&idle_notifier, n);
25 }
26 EXPORT_SYMBOL_GPL(idle_notifier_register);
27
28 void idle_notifier_unregister(struct notifier_block *n)
29 {
30         atomic_notifier_chain_unregister(&idle_notifier, n);
31 }
32 EXPORT_SYMBOL_GPL(idle_notifier_unregister);
33
34 void idle_notifier_call_chain(unsigned long val)
35 {
36         atomic_notifier_call_chain(&idle_notifier, val, NULL);
37 }
38 EXPORT_SYMBOL_GPL(idle_notifier_call_chain);
39
40 #ifdef CONFIG_SMP
41 /* Serializes the updates to cpu_online_mask, cpu_present_mask */
42 static DEFINE_MUTEX(cpu_add_remove_lock);
43
44 /*
45  * The following two API's must be used when attempting
46  * to serialize the updates to cpu_online_mask, cpu_present_mask.
47  */
48 void cpu_maps_update_begin(void)
49 {
50         mutex_lock(&cpu_add_remove_lock);
51 }
52
53 void cpu_maps_update_done(void)
54 {
55         mutex_unlock(&cpu_add_remove_lock);
56 }
57
58 static RAW_NOTIFIER_HEAD(cpu_chain);
59
60 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
61  * Should always be manipulated under cpu_add_remove_lock
62  */
63 static int cpu_hotplug_disabled;
64
65 #ifdef CONFIG_HOTPLUG_CPU
66
67 static struct {
68         struct task_struct *active_writer;
69         struct mutex lock; /* Synchronizes accesses to refcount, */
70         /*
71          * Also blocks the new readers during
72          * an ongoing cpu hotplug operation.
73          */
74         int refcount;
75 } cpu_hotplug = {
76         .active_writer = NULL,
77         .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
78         .refcount = 0,
79 };
80
81 void get_online_cpus(void)
82 {
83         might_sleep();
84         if (cpu_hotplug.active_writer == current)
85                 return;
86         mutex_lock(&cpu_hotplug.lock);
87         cpu_hotplug.refcount++;
88         mutex_unlock(&cpu_hotplug.lock);
89
90 }
91 EXPORT_SYMBOL_GPL(get_online_cpus);
92
93 void put_online_cpus(void)
94 {
95         if (cpu_hotplug.active_writer == current)
96                 return;
97         mutex_lock(&cpu_hotplug.lock);
98         if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
99                 wake_up_process(cpu_hotplug.active_writer);
100         mutex_unlock(&cpu_hotplug.lock);
101
102 }
103 EXPORT_SYMBOL_GPL(put_online_cpus);
104
105 /*
106  * This ensures that the hotplug operation can begin only when the
107  * refcount goes to zero.
108  *
109  * Note that during a cpu-hotplug operation, the new readers, if any,
110  * will be blocked by the cpu_hotplug.lock
111  *
112  * Since cpu_hotplug_begin() is always called after invoking
113  * cpu_maps_update_begin(), we can be sure that only one writer is active.
114  *
115  * Note that theoretically, there is a possibility of a livelock:
116  * - Refcount goes to zero, last reader wakes up the sleeping
117  *   writer.
118  * - Last reader unlocks the cpu_hotplug.lock.
119  * - A new reader arrives at this moment, bumps up the refcount.
120  * - The writer acquires the cpu_hotplug.lock finds the refcount
121  *   non zero and goes to sleep again.
122  *
123  * However, this is very difficult to achieve in practice since
124  * get_online_cpus() not an api which is called all that often.
125  *
126  */
127 static void cpu_hotplug_begin(void)
128 {
129         cpu_hotplug.active_writer = current;
130
131         for (;;) {
132                 mutex_lock(&cpu_hotplug.lock);
133                 if (likely(!cpu_hotplug.refcount))
134                         break;
135                 __set_current_state(TASK_UNINTERRUPTIBLE);
136                 mutex_unlock(&cpu_hotplug.lock);
137                 schedule();
138         }
139 }
140
141 static void cpu_hotplug_done(void)
142 {
143         cpu_hotplug.active_writer = NULL;
144         mutex_unlock(&cpu_hotplug.lock);
145 }
146
147 #else /* #if CONFIG_HOTPLUG_CPU */
148 static void cpu_hotplug_begin(void) {}
149 static void cpu_hotplug_done(void) {}
150 #endif  /* #else #if CONFIG_HOTPLUG_CPU */
151
152 /* Need to know about CPUs going up/down? */
153 int __ref register_cpu_notifier(struct notifier_block *nb)
154 {
155         int ret;
156         cpu_maps_update_begin();
157         ret = raw_notifier_chain_register(&cpu_chain, nb);
158         cpu_maps_update_done();
159         return ret;
160 }
161
162 static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
163                         int *nr_calls)
164 {
165         int ret;
166
167         ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
168                                         nr_calls);
169
170         return notifier_to_errno(ret);
171 }
172
173 static int cpu_notify(unsigned long val, void *v)
174 {
175         return __cpu_notify(val, v, -1, NULL);
176 }
177
178 #ifdef CONFIG_HOTPLUG_CPU
179
180 static void cpu_notify_nofail(unsigned long val, void *v)
181 {
182         BUG_ON(cpu_notify(val, v));
183 }
184 EXPORT_SYMBOL(register_cpu_notifier);
185
186 void __ref unregister_cpu_notifier(struct notifier_block *nb)
187 {
188         cpu_maps_update_begin();
189         raw_notifier_chain_unregister(&cpu_chain, nb);
190         cpu_maps_update_done();
191 }
192 EXPORT_SYMBOL(unregister_cpu_notifier);
193
194 static inline void check_for_tasks(int cpu)
195 {
196         struct task_struct *p;
197
198         write_lock_irq(&tasklist_lock);
199         for_each_process(p) {
200                 if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
201                     (p->utime || p->stime))
202                         printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d "
203                                 "(state = %ld, flags = %x)\n",
204                                 p->comm, task_pid_nr(p), cpu,
205                                 p->state, p->flags);
206         }
207         write_unlock_irq(&tasklist_lock);
208 }
209
210 struct take_cpu_down_param {
211         unsigned long mod;
212         void *hcpu;
213 };
214
215 /* Take this CPU down. */
216 static int __ref take_cpu_down(void *_param)
217 {
218         struct take_cpu_down_param *param = _param;
219         int err;
220
221         /* Ensure this CPU doesn't handle any more interrupts. */
222         err = __cpu_disable();
223         if (err < 0)
224                 return err;
225
226         cpu_notify(CPU_DYING | param->mod, param->hcpu);
227         return 0;
228 }
229
230 /* Requires cpu_add_remove_lock to be held */
231 static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
232 {
233         int err, nr_calls = 0;
234         void *hcpu = (void *)(long)cpu;
235         unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
236         struct take_cpu_down_param tcd_param = {
237                 .mod = mod,
238                 .hcpu = hcpu,
239         };
240
241         if (num_online_cpus() == 1)
242                 return -EBUSY;
243
244         if (!cpu_online(cpu))
245                 return -EINVAL;
246
247         cpu_hotplug_begin();
248
249         err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
250         if (err) {
251                 nr_calls--;
252                 __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
253                 printk("%s: attempt to take down CPU %u failed\n",
254                                 __func__, cpu);
255                 goto out_release;
256         }
257
258         err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
259         if (err) {
260                 /* CPU didn't die: tell everyone.  Can't complain. */
261                 cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
262
263                 goto out_release;
264         }
265         BUG_ON(cpu_online(cpu));
266
267         /*
268          * The migration_call() CPU_DYING callback will have removed all
269          * runnable tasks from the cpu, there's only the idle task left now
270          * that the migration thread is done doing the stop_machine thing.
271          *
272          * Wait for the stop thread to go away.
273          */
274         while (!idle_cpu(cpu))
275                 cpu_relax();
276
277         /* This actually kills the CPU. */
278         __cpu_die(cpu);
279
280         /* CPU is completely dead: tell everyone.  Too late to complain. */
281         cpu_notify_nofail(CPU_DEAD | mod, hcpu);
282
283         check_for_tasks(cpu);
284
285 out_release:
286         cpu_hotplug_done();
287         if (!err)
288                 cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
289         return err;
290 }
291
292 int __ref cpu_down(unsigned int cpu)
293 {
294         int err;
295
296         cpu_maps_update_begin();
297
298         if (cpu_hotplug_disabled) {
299                 err = -EBUSY;
300                 goto out;
301         }
302
303         err = _cpu_down(cpu, 0);
304
305 out:
306         cpu_maps_update_done();
307         return err;
308 }
309 EXPORT_SYMBOL(cpu_down);
310 #endif /*CONFIG_HOTPLUG_CPU*/
311
312 /* Requires cpu_add_remove_lock to be held */
313 static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
314 {
315         int ret, nr_calls = 0;
316         void *hcpu = (void *)(long)cpu;
317         unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
318
319         if (cpu_online(cpu) || !cpu_present(cpu))
320                 return -EINVAL;
321
322         cpu_hotplug_begin();
323         ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
324         if (ret) {
325                 nr_calls--;
326                 printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
327                                 __func__, cpu);
328                 goto out_notify;
329         }
330
331         /* Arch-specific enabling code. */
332         ret = __cpu_up(cpu);
333         if (ret != 0)
334                 goto out_notify;
335         BUG_ON(!cpu_online(cpu));
336
337         /* Now call notifier in preparation. */
338         cpu_notify(CPU_ONLINE | mod, hcpu);
339
340 out_notify:
341         if (ret != 0)
342                 __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
343         cpu_hotplug_done();
344
345         return ret;
346 }
347
348 int __cpuinit cpu_up(unsigned int cpu)
349 {
350         int err = 0;
351
352 #ifdef  CONFIG_MEMORY_HOTPLUG
353         int nid;
354         pg_data_t       *pgdat;
355 #endif
356
357         if (!cpu_possible(cpu)) {
358                 printk(KERN_ERR "can't online cpu %d because it is not "
359                         "configured as may-hotadd at boot time\n", cpu);
360 #if defined(CONFIG_IA64)
361                 printk(KERN_ERR "please check additional_cpus= boot "
362                                 "parameter\n");
363 #endif
364                 return -EINVAL;
365         }
366
367 #ifdef  CONFIG_MEMORY_HOTPLUG
368         nid = cpu_to_node(cpu);
369         if (!node_online(nid)) {
370                 err = mem_online_node(nid);
371                 if (err)
372                         return err;
373         }
374
375         pgdat = NODE_DATA(nid);
376         if (!pgdat) {
377                 printk(KERN_ERR
378                         "Can't online cpu %d due to NULL pgdat\n", cpu);
379                 return -ENOMEM;
380         }
381
382         if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
383                 mutex_lock(&zonelists_mutex);
384                 build_all_zonelists(NULL);
385                 mutex_unlock(&zonelists_mutex);
386         }
387 #endif
388
389         cpu_maps_update_begin();
390
391         if (cpu_hotplug_disabled) {
392                 err = -EBUSY;
393                 goto out;
394         }
395
396         err = _cpu_up(cpu, 0);
397
398 out:
399         cpu_maps_update_done();
400         return err;
401 }
402 EXPORT_SYMBOL_GPL(cpu_up);
403
404 #ifdef CONFIG_PM_SLEEP_SMP
405 static cpumask_var_t frozen_cpus;
406
407 void __weak arch_disable_nonboot_cpus_begin(void)
408 {
409 }
410
411 void __weak arch_disable_nonboot_cpus_end(void)
412 {
413 }
414
415 int disable_nonboot_cpus(void)
416 {
417         int cpu, first_cpu, error = 0;
418
419         cpu_maps_update_begin();
420         first_cpu = cpumask_first(cpu_online_mask);
421         /*
422          * We take down all of the non-boot CPUs in one shot to avoid races
423          * with the userspace trying to use the CPU hotplug at the same time
424          */
425         cpumask_clear(frozen_cpus);
426         arch_disable_nonboot_cpus_begin();
427
428         printk("Disabling non-boot CPUs ...\n");
429         for_each_online_cpu(cpu) {
430                 if (cpu == first_cpu)
431                         continue;
432                 error = _cpu_down(cpu, 1);
433                 if (!error)
434                         cpumask_set_cpu(cpu, frozen_cpus);
435                 else {
436                         printk(KERN_ERR "Error taking CPU%d down: %d\n",
437                                 cpu, error);
438                         break;
439                 }
440         }
441
442         arch_disable_nonboot_cpus_end();
443
444         if (!error) {
445                 BUG_ON(num_online_cpus() > 1);
446                 /* Make sure the CPUs won't be enabled by someone else */
447                 cpu_hotplug_disabled = 1;
448         } else {
449                 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
450         }
451         cpu_maps_update_done();
452         return error;
453 }
454
455 void __weak arch_enable_nonboot_cpus_begin(void)
456 {
457 }
458
459 void __weak arch_enable_nonboot_cpus_end(void)
460 {
461 }
462
463 void __ref enable_nonboot_cpus(void)
464 {
465         int cpu, error;
466
467         /* Allow everyone to use the CPU hotplug again */
468         cpu_maps_update_begin();
469         cpu_hotplug_disabled = 0;
470         if (cpumask_empty(frozen_cpus))
471                 goto out;
472
473         printk(KERN_INFO "Enabling non-boot CPUs ...\n");
474
475         arch_enable_nonboot_cpus_begin();
476
477         for_each_cpu(cpu, frozen_cpus) {
478                 error = _cpu_up(cpu, 1);
479                 if (!error) {
480                         printk(KERN_INFO "CPU%d is up\n", cpu);
481                         continue;
482                 }
483                 printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
484         }
485
486         arch_enable_nonboot_cpus_end();
487
488         cpumask_clear(frozen_cpus);
489 out:
490         cpu_maps_update_done();
491 }
492
493 static int __init alloc_frozen_cpus(void)
494 {
495         if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
496                 return -ENOMEM;
497         return 0;
498 }
499 core_initcall(alloc_frozen_cpus);
500
501 /*
502  * Prevent regular CPU hotplug from racing with the freezer, by disabling CPU
503  * hotplug when tasks are about to be frozen. Also, don't allow the freezer
504  * to continue until any currently running CPU hotplug operation gets
505  * completed.
506  * To modify the 'cpu_hotplug_disabled' flag, we need to acquire the
507  * 'cpu_add_remove_lock'. And this same lock is also taken by the regular
508  * CPU hotplug path and released only after it is complete. Thus, we
509  * (and hence the freezer) will block here until any currently running CPU
510  * hotplug operation gets completed.
511  */
512 void cpu_hotplug_disable_before_freeze(void)
513 {
514         cpu_maps_update_begin();
515         cpu_hotplug_disabled = 1;
516         cpu_maps_update_done();
517 }
518
519
520 /*
521  * When tasks have been thawed, re-enable regular CPU hotplug (which had been
522  * disabled while beginning to freeze tasks).
523  */
524 void cpu_hotplug_enable_after_thaw(void)
525 {
526         cpu_maps_update_begin();
527         cpu_hotplug_disabled = 0;
528         cpu_maps_update_done();
529 }
530
531 /*
532  * When callbacks for CPU hotplug notifications are being executed, we must
533  * ensure that the state of the system with respect to the tasks being frozen
534  * or not, as reported by the notification, remains unchanged *throughout the
535  * duration* of the execution of the callbacks.
536  * Hence we need to prevent the freezer from racing with regular CPU hotplug.
537  *
538  * This synchronization is implemented by mutually excluding regular CPU
539  * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
540  * Hibernate notifications.
541  */
542 static int
543 cpu_hotplug_pm_callback(struct notifier_block *nb,
544                         unsigned long action, void *ptr)
545 {
546         switch (action) {
547
548         case PM_SUSPEND_PREPARE:
549         case PM_HIBERNATION_PREPARE:
550                 cpu_hotplug_disable_before_freeze();
551                 break;
552
553         case PM_POST_SUSPEND:
554         case PM_POST_HIBERNATION:
555                 cpu_hotplug_enable_after_thaw();
556                 break;
557
558         default:
559                 return NOTIFY_DONE;
560         }
561
562         return NOTIFY_OK;
563 }
564
565
566 static int __init cpu_hotplug_pm_sync_init(void)
567 {
568         pm_notifier(cpu_hotplug_pm_callback, 0);
569         return 0;
570 }
571 core_initcall(cpu_hotplug_pm_sync_init);
572
573 #endif /* CONFIG_PM_SLEEP_SMP */
574
575 /**
576  * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
577  * @cpu: cpu that just started
578  *
579  * This function calls the cpu_chain notifiers with CPU_STARTING.
580  * It must be called by the arch code on the new cpu, before the new cpu
581  * enables interrupts and before the "boot" cpu returns from __cpu_up().
582  */
583 void __cpuinit notify_cpu_starting(unsigned int cpu)
584 {
585         unsigned long val = CPU_STARTING;
586
587 #ifdef CONFIG_PM_SLEEP_SMP
588         if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
589                 val = CPU_STARTING_FROZEN;
590 #endif /* CONFIG_PM_SLEEP_SMP */
591         cpu_notify(val, (void *)(long)cpu);
592 }
593
594 #endif /* CONFIG_SMP */
595
596 /*
597  * cpu_bit_bitmap[] is a special, "compressed" data structure that
598  * represents all NR_CPUS bits binary values of 1<<nr.
599  *
600  * It is used by cpumask_of() to get a constant address to a CPU
601  * mask value that has a single bit set only.
602  */
603
604 /* cpu_bit_bitmap[0] is empty - so we can back into it */
605 #define MASK_DECLARE_1(x)       [x+1][0] = (1UL << (x))
606 #define MASK_DECLARE_2(x)       MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
607 #define MASK_DECLARE_4(x)       MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
608 #define MASK_DECLARE_8(x)       MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
609
610 const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
611
612         MASK_DECLARE_8(0),      MASK_DECLARE_8(8),
613         MASK_DECLARE_8(16),     MASK_DECLARE_8(24),
614 #if BITS_PER_LONG > 32
615         MASK_DECLARE_8(32),     MASK_DECLARE_8(40),
616         MASK_DECLARE_8(48),     MASK_DECLARE_8(56),
617 #endif
618 };
619 EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
620
621 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
622 EXPORT_SYMBOL(cpu_all_bits);
623
624 #ifdef CONFIG_INIT_ALL_POSSIBLE
625 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
626         = CPU_BITS_ALL;
627 #else
628 static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
629 #endif
630 const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
631 EXPORT_SYMBOL(cpu_possible_mask);
632
633 static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
634 const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
635 EXPORT_SYMBOL(cpu_online_mask);
636
637 static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
638 const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
639 EXPORT_SYMBOL(cpu_present_mask);
640
641 static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
642 const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
643 EXPORT_SYMBOL(cpu_active_mask);
644
645 void set_cpu_possible(unsigned int cpu, bool possible)
646 {
647         if (possible)
648                 cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
649         else
650                 cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
651 }
652
653 void set_cpu_present(unsigned int cpu, bool present)
654 {
655         if (present)
656                 cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
657         else
658                 cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
659 }
660
661 void set_cpu_online(unsigned int cpu, bool online)
662 {
663         if (online)
664                 cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
665         else
666                 cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
667 }
668
669 void set_cpu_active(unsigned int cpu, bool active)
670 {
671         if (active)
672                 cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
673         else
674                 cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
675 }
676
677 void init_cpu_present(const struct cpumask *src)
678 {
679         cpumask_copy(to_cpumask(cpu_present_bits), src);
680 }
681
682 void init_cpu_possible(const struct cpumask *src)
683 {
684         cpumask_copy(to_cpumask(cpu_possible_bits), src);
685 }
686
687 void init_cpu_online(const struct cpumask *src)
688 {
689         cpumask_copy(to_cpumask(cpu_online_bits), src);
690 }