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>
14 /* This is a lot more paranoid now, since Linux can NOT handle
15 * being told there is a CPU when none exists. So any errors
16 * will return 0, meaning no CPU.
18 * We actually handling that case by noting which cpus startup
19 * and not telling anyone about the ones that dont.
21 static int lapic_start_cpu(unsigned long apicid)
24 unsigned long send_status, accept_status, start_eip;
25 int j, num_starts, maxlvt;
26 extern char _secondary_start[];
29 * Starting actual IPI sequence...
32 printk_spew("Asserting INIT.\n");
35 * Turn INIT on target chip
37 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
43 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_INT_ASSERT
46 printk_spew("Waiting for send to finish...\n");
51 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
52 } while (send_status && (timeout++ < 1000));
53 if (timeout >= 1000) {
54 printk_err("CPU %d: First apic write timed out. Disabling\n",
57 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
58 if (lapic_read(LAPIC_ESR)) {
59 printk_err("Try to reset ESR\n");
60 lapic_write_around(LAPIC_ESR, 0);
61 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
67 printk_spew("Deasserting INIT.\n");
70 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
73 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT);
75 printk_spew("Waiting for send to finish...\n");
80 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
81 } while (send_status && (timeout++ < 1000));
82 if (timeout >= 1000) {
83 printk_err("CPU %d: Second apic write timed out. Disabling\n",
89 start_eip = (unsigned long)_secondary_start;
90 printk_spew("start_eip=0x%08lx\n", start_eip);
95 * Run STARTUP IPI loop.
97 printk_spew("#startup loops: %d.\n", num_starts);
101 for (j = 1; j <= num_starts; j++) {
102 printk_spew("Sending STARTUP #%d to %u.\n", j, apicid);
103 lapic_read_around(LAPIC_SPIV);
104 lapic_write(LAPIC_ESR, 0);
105 lapic_read(LAPIC_ESR);
106 printk_spew("After apic_write.\n");
113 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
115 /* Boot on the stack */
116 /* Kick the second */
117 lapic_write_around(LAPIC_ICR, LAPIC_DM_STARTUP
118 | (start_eip >> 12));
121 * Give the other CPU some time to accept the IPI.
125 printk_spew("Startup point 1.\n");
127 printk_spew("Waiting for send to finish...\n");
132 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
133 } while (send_status && (timeout++ < 1000));
136 * Give the other CPU some time to accept the IPI.
140 * Due to the Pentium erratum 3AP.
143 lapic_read_around(LAPIC_SPIV);
144 lapic_write(LAPIC_ESR, 0);
146 accept_status = (lapic_read(LAPIC_ESR) & 0xEF);
147 if (send_status || accept_status)
150 printk_spew("After Startup.\n");
152 printk_warning("APIC never delivered???\n");
154 printk_warning("APIC delivery error (%lx).\n", accept_status);
155 if (send_status || accept_status)
160 /* Number of cpus that are currently running in linuxbios */
161 static atomic_t active_cpus = ATOMIC_INIT(1);
163 /* start_cpu_lock covers last_cpu_index and secondary_stack.
164 * Only starting one cpu at a time let's me remove the logic
165 * for select the stack from assembly language.
167 * In addition communicating by variables to the cpu I
168 * am starting allows me to veryify it has started before
172 static spinlock_t start_cpu_lock = SPIN_LOCK_UNLOCKED;
173 static unsigned last_cpu_index = 0;
174 volatile unsigned long secondary_stack;
176 int start_cpu(device_t cpu)
178 extern unsigned char _estack[];
179 struct cpu_info *info;
180 unsigned long stack_end;
181 unsigned long apicid;
186 spin_lock(&start_cpu_lock);
188 /* Get the cpu's apicid */
189 apicid = cpu->path.u.apic.apic_id;
191 /* Get an index for the new processor */
192 index = ++last_cpu_index;
194 /* Find end of the new processors stack */
195 stack_end = ((unsigned long)_estack) - (STACK_SIZE*index) - sizeof(struct cpu_info);
197 /* Record the index and which cpu structure we are using */
198 info = (struct cpu_info *)stack_end;
202 /* Advertise the new stack to start_cpu */
203 secondary_stack = stack_end;
205 /* Until the cpu starts up report the cpu is not enabled */
207 cpu->initialized = 0;
210 result = lapic_start_cpu(apicid);
214 /* Wait 1s or until the new the new cpu calls in */
215 for(count = 0; count < 100000 ; count++) {
216 if (secondary_stack == 0) {
224 spin_unlock(&start_cpu_lock);
228 /* C entry point of secondary cpus */
229 void secondary_cpu_init(void)
231 atomic_inc(&active_cpus);
232 #if CONFIG_MAX_CPUS>2
233 spin_lock(&start_cpu_lock);
236 #if CONFIG_MAX_CPUS>2
237 spin_unlock(&start_cpu_lock);
239 atomic_dec(&active_cpus);
243 static void initialize_other_cpus(struct bus *cpu_bus)
245 int old_active_count, active_count;
247 /* Loop through the cpus once getting them started */
248 for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) {
249 if (cpu->path.type != DEVICE_PATH_APIC) {
255 if (cpu->initialized) {
258 if (!start_cpu(cpu)) {
259 /* Record the error in cpu? */
260 printk_err("CPU %u would not start!\n",
261 cpu->path.u.apic.apic_id);
263 #if CONFIG_MAX_CPUS>2
268 /* Now loop until the other cpus have finished initializing */
269 old_active_count = 1;
270 active_count = atomic_read(&active_cpus);
271 while(active_count > 1) {
272 if (active_count != old_active_count) {
273 printk_info("Waiting for %d CPUS to stop\n", active_count - 1);
274 old_active_count = active_count;
277 active_count = atomic_read(&active_cpus);
279 for(cpu = cpu_bus->children; cpu; cpu = cpu->sibling) {
280 if (cpu->path.type != DEVICE_PATH_APIC) {
283 if (!cpu->initialized) {
284 printk_err("CPU %u did not initialize!\n",
285 cpu->path.u.apic.apic_id);
286 #warning "FIXME do I need a mainboard_cpu_fixup function?"
289 printk_debug("All AP CPUs stopped\n");
292 #else /* CONFIG_SMP */
293 #define initialize_other_cpus(root) do {} while(0)
294 #endif /* CONFIG_SMP */
296 void initialize_cpus(struct bus *cpu_bus)
298 struct device_path cpu_path;
299 struct cpu_info *info;
301 /* Find the info struct for this cpu */
305 /* Ensure the local apic is enabled */
308 /* Get the device path of the boot cpu */
309 cpu_path.type = DEVICE_PATH_APIC;
310 cpu_path.u.apic.apic_id = lapicid();
312 /* Get the device path of the boot cpu */
313 cpu_path.type = DEVICE_PATH_CPU;
314 cpu_path.u.cpu.id = 0;
317 /* Find the device structure for the boot cpu */
318 info->cpu = alloc_find_dev(cpu_bus, &cpu_path);
320 /* Initialize the bootstrap processor */
323 /* Now initialize the rest of the cpus */
324 initialize_other_cpus(cpu_bus);