blob: 98f60c5caa1bef91733a4c0bcc646da4fa0b7a24 [file] [log] [blame]
Thomas Gleixner38498a62012-04-20 13:05:44 +00001/*
2 * Common SMP CPU bringup/teardown functions
3 */
Thomas Gleixner29d5e042012-04-20 13:05:45 +00004#include <linux/err.h>
5#include <linux/smp.h>
Thomas Gleixner38498a62012-04-20 13:05:44 +00006#include <linux/init.h>
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007#include <linux/sched.h>
8#include <linux/percpu.h>
Thomas Gleixner38498a62012-04-20 13:05:44 +00009
10#include "smpboot.h"
11
Thomas Gleixner29d5e042012-04-20 13:05:45 +000012#ifdef CONFIG_GENERIC_SMP_IDLE_THREAD
Thomas Gleixner29d5e042012-04-20 13:05:45 +000013/*
14 * For the hotplug case we keep the task structs around and reuse
15 * them.
16 */
17static DEFINE_PER_CPU(struct task_struct *, idle_threads);
18
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070019struct task_struct * __cpuinit idle_thread_get(unsigned int cpu)
Thomas Gleixner29d5e042012-04-20 13:05:45 +000020{
21 struct task_struct *tsk = per_cpu(idle_threads, cpu);
22
23 if (!tsk)
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070024 return ERR_PTR(-ENOMEM);
Thomas Gleixner29d5e042012-04-20 13:05:45 +000025 init_idle(tsk, cpu);
26 return tsk;
27}
28
Thomas Gleixner29d5e042012-04-20 13:05:45 +000029void __init idle_thread_set_boot_cpu(void)
30{
31 per_cpu(idle_threads, smp_processor_id()) = current;
32}
33
Srivatsa S. Bhat4a70d2d2012-05-24 20:41:00 +053034/**
35 * idle_init - Initialize the idle thread for a cpu
36 * @cpu: The cpu for which the idle thread should be initialized
37 *
38 * Creates the thread if it does not exist.
39 */
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070040static inline void idle_init(unsigned int cpu)
41{
42 struct task_struct *tsk = per_cpu(idle_threads, cpu);
43
44 if (!tsk) {
45 tsk = fork_idle(cpu);
46 if (IS_ERR(tsk))
47 pr_err("SMP: fork_idle() failed for CPU %u\n", cpu);
48 else
49 per_cpu(idle_threads, cpu) = tsk;
50 }
51}
52
Thomas Gleixner29d5e042012-04-20 13:05:45 +000053/**
Srivatsa S. Bhat4a70d2d2012-05-24 20:41:00 +053054 * idle_threads_init - Initialize idle threads for all cpus
Thomas Gleixner29d5e042012-04-20 13:05:45 +000055 */
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070056void __init idle_threads_init(void)
Thomas Gleixner29d5e042012-04-20 13:05:45 +000057{
Srivatsa S. Bhatee74d132012-05-24 20:40:55 +053058 unsigned int cpu, boot_cpu;
59
60 boot_cpu = smp_processor_id();
Thomas Gleixner29d5e042012-04-20 13:05:45 +000061
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070062 for_each_possible_cpu(cpu) {
Srivatsa S. Bhatee74d132012-05-24 20:40:55 +053063 if (cpu != boot_cpu)
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070064 idle_init(cpu);
Thomas Gleixner29d5e042012-04-20 13:05:45 +000065 }
Thomas Gleixner29d5e042012-04-20 13:05:45 +000066}
Thomas Gleixner29d5e042012-04-20 13:05:45 +000067#endif