1 #include <cpu/x86/lapic.h>
4 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/path.h>
8 #include <smp/atomic.h>
9 #include <smp/spinlock.h>
15 /* This is a lot more paranoid now, since Linux can NOT handle
16 * being told there is a CPU when none exists. So any errors
17 * will return 0, meaning no CPU.
19 * We actually handling that case by noting which cpus startup
20 * and not telling anyone about the ones that dont.
22 static int lapic_start_cpu(unsigned long apicid)
25 unsigned long send_status, accept_status, start_eip;
26 int j, num_starts, maxlvt;
27 extern char _secondary_start[];
30 * Starting actual IPI sequence...
33 printk_spew("Asserting INIT.\n");
36 * Turn INIT on target chip
38 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
44 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_INT_ASSERT
47 printk_spew("Waiting for send to finish...\n");
52 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
53 } while (send_status && (timeout++ < 1000));
54 if (timeout >= 1000) {
55 printk_err("CPU %d: First apic write timed out. Disabling\n",
58 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
59 if (lapic_read(LAPIC_ESR)) {
60 printk_err("Try to reset ESR\n");
61 lapic_write_around(LAPIC_ESR, 0);
62 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
68 printk_spew("Deasserting INIT.\n");
71 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
74 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT);
76 printk_spew("Waiting for send to finish...\n");
81 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
82 } while (send_status && (timeout++ < 1000));
83 if (timeout >= 1000) {
84 printk_err("CPU %d: Second apic write timed out. Disabling\n",
90 start_eip = (unsigned long)_secondary_start;
91 printk_spew("start_eip=0x%08lx\n", start_eip);
96 * Run STARTUP IPI loop.
98 printk_spew("#startup loops: %d.\n", num_starts);
102 for (j = 1; j <= num_starts; j++) {
103 printk_spew("Sending STARTUP #%d to %u.\n", j, apicid);
104 lapic_read_around(LAPIC_SPIV);
105 lapic_write(LAPIC_ESR, 0);
106 lapic_read(LAPIC_ESR);
107 printk_spew("After apic_write.\n");
114 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
116 /* Boot on the stack */
117 /* Kick the second */
118 lapic_write_around(LAPIC_ICR, LAPIC_DM_STARTUP
119 | (start_eip >> 12));
122 * Give the other CPU some time to accept the IPI.
126 printk_spew("Startup point 1.\n");
128 printk_spew("Waiting for send to finish...\n");
133 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
134 } while (send_status && (timeout++ < 1000));
137 * Give the other CPU some time to accept the IPI.
141 * Due to the Pentium erratum 3AP.
144 lapic_read_around(LAPIC_SPIV);
145 lapic_write(LAPIC_ESR, 0);
147 accept_status = (lapic_read(LAPIC_ESR) & 0xEF);
148 if (send_status || accept_status)
151 printk_spew("After Startup.\n");
153 printk_warning("APIC never delivered???\n");
155 printk_warning("APIC delivery error (%lx).\n", accept_status);
156 if (send_status || accept_status)
161 /* Number of cpus that are currently running in linuxbios */
162 static atomic_t active_cpus = ATOMIC_INIT(1);
164 /* start_cpu_lock covers last_cpu_index and secondary_stack.
165 * Only starting one cpu at a time let's me remove the logic
166 * for select the stack from assembly language.
168 * In addition communicating by variables to the cpu I
169 * am starting allows me to veryify it has started before
173 static spinlock_t start_cpu_lock = SPIN_LOCK_UNLOCKED;
174 static unsigned last_cpu_index = 0;
175 volatile unsigned long secondary_stack;
177 int start_cpu(device_t cpu)
179 extern unsigned char _estack[];
180 struct cpu_info *info;
181 unsigned long stack_end;
182 unsigned long apicid;
187 spin_lock(&start_cpu_lock);
189 /* Get the cpu's apicid */
190 apicid = cpu->path.u.apic.apic_id;
192 /* Get an index for the new processor */
193 index = ++last_cpu_index;
195 /* Find end of the new processors stack */
196 stack_end = ((unsigned long)_estack) - (STACK_SIZE*index) - sizeof(struct cpu_info);
198 /* Record the index and which cpu structure we are using */
199 info = (struct cpu_info *)stack_end;
203 /* Advertise the new stack to start_cpu */
204 secondary_stack = stack_end;
206 /* Until the cpu starts up report the cpu is not enabled */
208 cpu->initialized = 0;
211 result = lapic_start_cpu(apicid);
215 /* Wait 1s or until the new the new cpu calls in */
216 for(count = 0; count < 100000 ; count++) {
217 if (secondary_stack == 0) {
225 spin_unlock(&start_cpu_lock);
229 /* C entry point of secondary cpus */
230 void secondary_cpu_init(void)
232 atomic_inc(&active_cpus);
233 #if SERIAL_CPU_INIT == 1
234 #if CONFIG_MAX_CPUS>2
235 spin_lock(&start_cpu_lock);
239 #if SERIAL_CPU_INIT == 1
240 #if CONFIG_MAX_CPUS>2
241 spin_unlock(&start_cpu_lock);
245 atomic_dec(&active_cpus);
249 static void initialize_other_cpus(struct bus *cpu_bus)
251 int old_active_count, active_count;
253 /* Loop through the cpus once getting them started */
254 for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) {
255 if (cpu->path.type != DEVICE_PATH_APIC) {
263 if (cpu->initialized) {
267 if (!start_cpu(cpu)) {
268 /* Record the error in cpu? */
269 printk_err("CPU %u would not start!\n",
270 cpu->path.u.apic.apic_id);
272 #if SERIAL_CPU_INIT == 1
273 #if CONFIG_MAX_CPUS>2
279 /* Now loop until the other cpus have finished initializing */
280 old_active_count = 1;
281 active_count = atomic_read(&active_cpus);
282 while(active_count > 1) {
283 if (active_count != old_active_count) {
284 printk_info("Waiting for %d CPUS to stop\n", active_count - 1);
285 old_active_count = active_count;
288 active_count = atomic_read(&active_cpus);
290 for(cpu = cpu_bus->children; cpu; cpu = cpu->sibling) {
291 if (cpu->path.type != DEVICE_PATH_APIC) {
294 if (!cpu->initialized) {
295 printk_err("CPU %u did not initialize!\n",
296 cpu->path.u.apic.apic_id);
297 #warning "FIXME do I need a mainboard_cpu_fixup function?"
300 printk_debug("All AP CPUs stopped\n");
303 #else /* CONFIG_SMP */
304 #define initialize_other_cpus(root) do {} while(0)
305 #endif /* CONFIG_SMP */
307 void initialize_cpus(struct bus *cpu_bus)
309 struct device_path cpu_path;
310 struct cpu_info *info;
312 /* Find the info struct for this cpu */
316 /* Ensure the local apic is enabled */
319 /* Get the device path of the boot cpu */
320 cpu_path.type = DEVICE_PATH_APIC;
321 cpu_path.u.apic.apic_id = lapicid();
323 /* Get the device path of the boot cpu */
324 cpu_path.type = DEVICE_PATH_CPU;
325 cpu_path.u.cpu.id = 0;
328 /* Find the device structure for the boot cpu */
329 info->cpu = alloc_find_dev(cpu_bus, &cpu_path);
331 /* Initialize the bootstrap processor */
334 /* Now initialize the rest of the cpus */
335 initialize_other_cpus(cpu_bus);