1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/sched.h>
20#include <linux/kernel_stat.h>
21#include <linux/bootmem.h>
22#include <linux/notifier.h>
23#include <linux/cpu.h>
24#include <linux/percpu.h>
25#include <linux/delay.h>
26#include <linux/err.h>
27#include <linux/irq.h>
28#include <asm/mmu_context.h>
29#include <asm/tlbflush.h>
30#include <asm/sections.h>
31
32
33static DEFINE_PER_CPU(int, cpu_state) = { 0 };
34
35
36unsigned long start_cpu_function_addr;
37
38
39void __init smp_prepare_boot_cpu(void)
40{
41 int cpu = smp_processor_id();
42 set_cpu_online(cpu, 1);
43 set_cpu_present(cpu, 1);
44 __this_cpu_write(cpu_state, CPU_ONLINE);
45
46 init_messaging();
47}
48
49static void start_secondary(void);
50
51
52
53
54
55
56void __init smp_prepare_cpus(unsigned int max_cpus)
57{
58 long rc;
59 int cpu, cpu_count;
60 int boot_cpu = smp_processor_id();
61
62 current_thread_info()->cpu = boot_cpu;
63
64
65
66
67
68 rc = sched_setaffinity(current->pid, cpumask_of(boot_cpu));
69 if (rc != 0)
70 pr_err("Couldn't set init affinity to boot cpu (%ld)\n", rc);
71
72
73 print_disabled_cpus();
74
75
76
77
78
79
80
81 start_cpu_function_addr = (unsigned long) &online_secondary;
82
83
84 cpu_count = 1;
85 for (cpu = 0; cpu < NR_CPUS; ++cpu) {
86 struct task_struct *idle;
87
88 if (cpu == boot_cpu)
89 continue;
90
91 if (!cpu_possible(cpu)) {
92
93
94
95
96
97 per_cpu(boot_sp, cpu) = 0;
98 per_cpu(boot_pc, cpu) = (unsigned long) smp_nap;
99 continue;
100 }
101
102
103 idle = fork_idle(cpu);
104 if (IS_ERR(idle))
105 panic("failed fork for CPU %d", cpu);
106 idle->thread.pc = (unsigned long) start_secondary;
107
108
109 per_cpu(boot_sp, cpu) = task_ksp0(idle);
110 per_cpu(boot_pc, cpu) = idle->thread.pc;
111
112 ++cpu_count;
113 }
114 BUG_ON(cpu_count > (max_cpus ? max_cpus : 1));
115
116
117 init_cpu_present(cpu_possible_mask);
118 if (cpumask_weight(cpu_present_mask) > 1) {
119 mb();
120 hv_start_all_tiles();
121 }
122}
123
124static __initdata struct cpumask init_affinity;
125
126static __init int reset_init_affinity(void)
127{
128 long rc = sched_setaffinity(current->pid, &init_affinity);
129 if (rc != 0)
130 pr_warn("couldn't reset init affinity (%ld)\n", rc);
131 return 0;
132}
133late_initcall(reset_init_affinity);
134
135static struct cpumask cpu_started;
136
137
138
139
140
141
142static void start_secondary(void)
143{
144 int cpuid;
145
146 preempt_disable();
147
148 cpuid = smp_processor_id();
149
150
151 set_my_cpu_offset(__per_cpu_offset[cpuid]);
152
153
154
155
156
157
158
159
160 __this_cpu_write(current_asid, min_asid);
161
162
163 atomic_inc(&init_mm.mm_count);
164 current->active_mm = &init_mm;
165 if (current->mm)
166 BUG();
167 enter_lazy_tlb(&init_mm, current);
168
169
170 init_messaging();
171 local_irq_enable();
172
173
174
175 if (cpumask_test_and_set_cpu(cpuid, &cpu_started)) {
176 pr_warn("CPU#%d already started!\n", cpuid);
177 for (;;)
178 local_irq_enable();
179 }
180
181 smp_nap();
182}
183
184
185
186
187void online_secondary(void)
188{
189
190
191
192
193 local_flush_tlb();
194
195 BUG_ON(in_interrupt());
196
197
198 wmb();
199
200 notify_cpu_starting(smp_processor_id());
201
202 set_cpu_online(smp_processor_id(), 1);
203 __this_cpu_write(cpu_state, CPU_ONLINE);
204
205
206 setup_cpu(0);
207
208
209 setup_tile_timer();
210
211 cpu_startup_entry(CPUHP_ONLINE);
212}
213
214int __cpu_up(unsigned int cpu, struct task_struct *tidle)
215{
216
217 static int timeout;
218 for (; !cpumask_test_cpu(cpu, &cpu_started); timeout++) {
219 if (timeout >= 50000) {
220 pr_info("skipping unresponsive cpu%d\n", cpu);
221 local_irq_enable();
222 return -EIO;
223 }
224 udelay(100);
225 }
226
227 local_irq_enable();
228 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
229
230
231 send_IPI_single(cpu, MSG_TAG_START_CPU);
232 while (!cpumask_test_cpu(cpu, cpu_online_mask))
233 cpu_relax();
234 return 0;
235}
236
237static void panic_start_cpu(void)
238{
239 panic("Received a MSG_START_CPU IPI after boot finished.");
240}
241
242void __init smp_cpus_done(unsigned int max_cpus)
243{
244 int cpu, next, rc;
245
246
247 start_cpu_function_addr = (unsigned long) &panic_start_cpu;
248
249 cpumask_copy(&init_affinity, cpu_online_mask);
250
251
252
253
254
255
256
257
258
259
260
261 for (cpu = cpumask_first(&init_affinity);
262 (next = cpumask_next(cpu, &init_affinity)) < nr_cpu_ids;
263 cpu = next)
264 ;
265 rc = sched_setaffinity(current->pid, cpumask_of(cpu));
266 if (rc != 0)
267 pr_err("Couldn't set init affinity to cpu %d (%d)\n", cpu, rc);
268}
269