| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * arch/sh/kernel/smp.c | 
|  | 3 | * | 
|  | 4 | * SMP support for the SuperH processors. | 
|  | 5 | * | 
|  | 6 | * Copyright (C) 2002, 2003 Paul Mundt | 
|  | 7 | * | 
|  | 8 | * This program is free software; you can redistribute it and/or modify it | 
|  | 9 | * under the terms of the GNU General Public License as published by the | 
|  | 10 | * Free Software Foundation; either version 2 of the License, or (at your | 
|  | 11 | * option) any later version. | 
|  | 12 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/cache.h> | 
|  | 14 | #include <linux/cpumask.h> | 
|  | 15 | #include <linux/delay.h> | 
|  | 16 | #include <linux/init.h> | 
|  | 17 | #include <linux/interrupt.h> | 
|  | 18 | #include <linux/spinlock.h> | 
|  | 19 | #include <linux/threads.h> | 
|  | 20 | #include <linux/module.h> | 
|  | 21 | #include <linux/time.h> | 
|  | 22 | #include <linux/timex.h> | 
|  | 23 | #include <linux/sched.h> | 
| David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 24 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | #include <asm/atomic.h> | 
|  | 27 | #include <asm/processor.h> | 
|  | 28 | #include <asm/system.h> | 
|  | 29 | #include <asm/mmu_context.h> | 
|  | 30 | #include <asm/smp.h> | 
|  | 31 |  | 
|  | 32 | /* | 
|  | 33 | * This was written with the Sega Saturn (SMP SH-2 7604) in mind, | 
|  | 34 | * but is designed to be usable regardless if there's an MMU | 
|  | 35 | * present or not. | 
|  | 36 | */ | 
|  | 37 | struct sh_cpuinfo cpu_data[NR_CPUS]; | 
|  | 38 |  | 
|  | 39 | extern void per_cpu_trap_init(void); | 
|  | 40 |  | 
|  | 41 | cpumask_t cpu_possible_map; | 
| David S. Miller | c8923c6 | 2005-10-13 14:41:23 -0700 | [diff] [blame] | 42 | EXPORT_SYMBOL(cpu_possible_map); | 
|  | 43 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | cpumask_t cpu_online_map; | 
| Greg Banks | e16b38f | 2006-10-02 02:17:40 -0700 | [diff] [blame] | 45 | EXPORT_SYMBOL(cpu_online_map); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static atomic_t cpus_booted = ATOMIC_INIT(0); | 
|  | 47 |  | 
|  | 48 | /* These are defined by the board-specific code. */ | 
|  | 49 |  | 
|  | 50 | /* | 
|  | 51 | * Cause the function described by call_data to be executed on the passed | 
|  | 52 | * cpu.  When the function has finished, increment the finished field of | 
|  | 53 | * call_data. | 
|  | 54 | */ | 
|  | 55 | void __smp_send_ipi(unsigned int cpu, unsigned int action); | 
|  | 56 |  | 
|  | 57 | /* | 
|  | 58 | * Find the number of available processors | 
|  | 59 | */ | 
|  | 60 | unsigned int __smp_probe_cpus(void); | 
|  | 61 |  | 
|  | 62 | /* | 
|  | 63 | * Start a particular processor | 
|  | 64 | */ | 
|  | 65 | void __smp_slave_init(unsigned int cpu); | 
|  | 66 |  | 
|  | 67 | /* | 
|  | 68 | * Run specified function on a particular processor. | 
|  | 69 | */ | 
|  | 70 | void __smp_call_function(unsigned int cpu); | 
|  | 71 |  | 
|  | 72 | static inline void __init smp_store_cpu_info(unsigned int cpu) | 
|  | 73 | { | 
|  | 74 | cpu_data[cpu].loops_per_jiffy = loops_per_jiffy; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void __init smp_prepare_cpus(unsigned int max_cpus) | 
|  | 78 | { | 
|  | 79 | unsigned int cpu = smp_processor_id(); | 
|  | 80 | int i; | 
|  | 81 |  | 
|  | 82 | atomic_set(&cpus_booted, 1); | 
|  | 83 | smp_store_cpu_info(cpu); | 
|  | 84 |  | 
|  | 85 | for (i = 0; i < __smp_probe_cpus(); i++) | 
|  | 86 | cpu_set(i, cpu_possible_map); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | void __devinit smp_prepare_boot_cpu(void) | 
|  | 90 | { | 
|  | 91 | unsigned int cpu = smp_processor_id(); | 
|  | 92 |  | 
|  | 93 | cpu_set(cpu, cpu_online_map); | 
|  | 94 | cpu_set(cpu, cpu_possible_map); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | int __cpu_up(unsigned int cpu) | 
|  | 98 | { | 
|  | 99 | struct task_struct *tsk; | 
|  | 100 |  | 
|  | 101 | tsk = fork_idle(cpu); | 
|  | 102 |  | 
|  | 103 | if (IS_ERR(tsk)) | 
|  | 104 | panic("Failed forking idle task for cpu %d\n", cpu); | 
|  | 105 |  | 
| Al Viro | cafcfca | 2006-01-12 01:05:45 -0800 | [diff] [blame] | 106 | task_thread_info(tsk)->cpu = cpu; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 |  | 
|  | 108 | cpu_set(cpu, cpu_online_map); | 
|  | 109 |  | 
|  | 110 | return 0; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | int start_secondary(void *unused) | 
|  | 114 | { | 
| Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 115 | unsigned int cpu; | 
|  | 116 |  | 
|  | 117 | cpu = smp_processor_id(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | atomic_inc(&init_mm.mm_count); | 
|  | 120 | current->active_mm = &init_mm; | 
|  | 121 |  | 
|  | 122 | smp_store_cpu_info(cpu); | 
|  | 123 |  | 
|  | 124 | __smp_slave_init(cpu); | 
| Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 125 | preempt_disable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | per_cpu_trap_init(); | 
|  | 127 |  | 
|  | 128 | atomic_inc(&cpus_booted); | 
|  | 129 |  | 
|  | 130 | cpu_idle(); | 
|  | 131 | return 0; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void __init smp_cpus_done(unsigned int max_cpus) | 
|  | 135 | { | 
|  | 136 | smp_mb(); | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | void smp_send_reschedule(int cpu) | 
|  | 140 | { | 
|  | 141 | __smp_send_ipi(cpu, SMP_MSG_RESCHEDULE); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | static void stop_this_cpu(void *unused) | 
|  | 145 | { | 
|  | 146 | cpu_clear(smp_processor_id(), cpu_online_map); | 
|  | 147 | local_irq_disable(); | 
|  | 148 |  | 
|  | 149 | for (;;) | 
|  | 150 | cpu_relax(); | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | void smp_send_stop(void) | 
|  | 154 | { | 
|  | 155 | smp_call_function(stop_this_cpu, 0, 1, 0); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 |  | 
|  | 159 | struct smp_fn_call_struct smp_fn_call = { | 
|  | 160 | .lock		= SPIN_LOCK_UNLOCKED, | 
|  | 161 | .finished	= ATOMIC_INIT(0), | 
|  | 162 | }; | 
|  | 163 |  | 
|  | 164 | /* | 
|  | 165 | * The caller of this wants the passed function to run on every cpu.  If wait | 
|  | 166 | * is set, wait until all cpus have finished the function before returning. | 
|  | 167 | * The lock is here to protect the call structure. | 
|  | 168 | * You must not call this function with disabled interrupts or from a | 
|  | 169 | * hardware interrupt handler or from a bottom half handler. | 
|  | 170 | */ | 
|  | 171 | int smp_call_function(void (*func)(void *info), void *info, int retry, int wait) | 
|  | 172 | { | 
|  | 173 | unsigned int nr_cpus = atomic_read(&cpus_booted); | 
|  | 174 | int i; | 
|  | 175 |  | 
|  | 176 | if (nr_cpus < 2) | 
|  | 177 | return 0; | 
|  | 178 |  | 
|  | 179 | /* Can deadlock when called with interrupts disabled */ | 
|  | 180 | WARN_ON(irqs_disabled()); | 
|  | 181 |  | 
|  | 182 | spin_lock(&smp_fn_call.lock); | 
|  | 183 |  | 
|  | 184 | atomic_set(&smp_fn_call.finished, 0); | 
|  | 185 | smp_fn_call.fn = func; | 
|  | 186 | smp_fn_call.data = info; | 
|  | 187 |  | 
|  | 188 | for (i = 0; i < nr_cpus; i++) | 
|  | 189 | if (i != smp_processor_id()) | 
|  | 190 | __smp_call_function(i); | 
|  | 191 |  | 
|  | 192 | if (wait) | 
|  | 193 | while (atomic_read(&smp_fn_call.finished) != (nr_cpus - 1)); | 
|  | 194 |  | 
|  | 195 | spin_unlock(&smp_fn_call.lock); | 
|  | 196 |  | 
|  | 197 | return 0; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | /* Not really SMP stuff ... */ | 
|  | 201 | int setup_profiling_timer(unsigned int multiplier) | 
|  | 202 | { | 
|  | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  |