Thomas Gleixner | 38498a6 | 2012-04-20 13:05:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common SMP CPU bringup/teardown functions |
| 3 | */ |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 4 | #include <linux/err.h> |
| 5 | #include <linux/smp.h> |
Thomas Gleixner | 38498a6 | 2012-04-20 13:05:44 +0000 | [diff] [blame] | 6 | #include <linux/init.h> |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 7 | #include <linux/sched.h> |
| 8 | #include <linux/percpu.h> |
Thomas Gleixner | 38498a6 | 2012-04-20 13:05:44 +0000 | [diff] [blame] | 9 | |
| 10 | #include "smpboot.h" |
| 11 | |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 12 | #ifdef CONFIG_GENERIC_SMP_IDLE_THREAD |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 13 | /* |
| 14 | * For the hotplug case we keep the task structs around and reuse |
| 15 | * them. |
| 16 | */ |
| 17 | static DEFINE_PER_CPU(struct task_struct *, idle_threads); |
| 18 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 19 | struct task_struct * __cpuinit idle_thread_get(unsigned int cpu) |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 20 | { |
| 21 | struct task_struct *tsk = per_cpu(idle_threads, cpu); |
| 22 | |
| 23 | if (!tsk) |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 24 | return ERR_PTR(-ENOMEM); |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 25 | init_idle(tsk, cpu); |
| 26 | return tsk; |
| 27 | } |
| 28 | |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 29 | void __init idle_thread_set_boot_cpu(void) |
| 30 | { |
| 31 | per_cpu(idle_threads, smp_processor_id()) = current; |
| 32 | } |
| 33 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 34 | static inline void idle_init(unsigned int cpu) |
| 35 | { |
| 36 | struct task_struct *tsk = per_cpu(idle_threads, cpu); |
| 37 | |
| 38 | if (!tsk) { |
| 39 | tsk = fork_idle(cpu); |
| 40 | if (IS_ERR(tsk)) |
| 41 | pr_err("SMP: fork_idle() failed for CPU %u\n", cpu); |
| 42 | else |
| 43 | per_cpu(idle_threads, cpu) = tsk; |
| 44 | } |
| 45 | } |
| 46 | |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 47 | /** |
| 48 | * idle_thread_init - Initialize the idle thread for a cpu |
| 49 | * @cpu: The cpu for which the idle thread should be initialized |
| 50 | * |
| 51 | * Creates the thread if it does not exist. |
| 52 | */ |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 53 | void __init idle_threads_init(void) |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 54 | { |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 55 | unsigned int cpu; |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 56 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 57 | for_each_possible_cpu(cpu) { |
| 58 | if (cpu != smp_processor_id()) |
| 59 | idle_init(cpu); |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 60 | } |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 61 | } |
Thomas Gleixner | 29d5e04 | 2012-04-20 13:05:45 +0000 | [diff] [blame] | 62 | #endif |