2 2005.12 yhlu add linuxbios_ram cross the vga font buffer handling
3 2005.12 yhlu add _RAMBASE above 1M support for SMP
6 #include <cpu/x86/lapic.h>
9 #include <console/console.h>
11 #include <device/device.h>
12 #include <device/path.h>
13 #include <smp/atomic.h>
14 #include <smp/spinlock.h>
19 /* This is a lot more paranoid now, since Linux can NOT handle
20 * being told there is a CPU when none exists. So any errors
21 * will return 0, meaning no CPU.
23 * We actually handling that case by noting which cpus startup
24 * and not telling anyone about the ones that dont.
26 static unsigned long get_valid_start_eip(unsigned long orig_start_eip)
28 return (unsigned long)orig_start_eip & 0xfffff; // 20 bit
31 static void copy_secondary_start_to_1m_below(void)
33 #if _RAMBASE > 0x100000
34 extern char _secondary_start[];
35 extern char _secondary_start_end[];
36 unsigned long code_size;
37 unsigned long start_eip;
39 /* _secondary_start need to be masked 20 above bit, because 16 bit code in secondary.S
40 Also We need to copy the _secondary_start to the below 1M region
42 start_eip = get_valid_start_eip((unsigned long)_secondary_start);
43 code_size = (unsigned long)_secondary_start_end - (unsigned long)_secondary_start;
45 /* copy the _secondary_start to the ram below 1M*/
46 memcpy(start_eip, (unsigned long)_secondary_start, code_size);
48 printk_debug("start_eip=0x%08lx, offset=0x%08lx, code_size=0x%08lx\n", start_eip, ((unsigned long)_secondary_start - start_eip), code_size);
52 static int lapic_start_cpu(unsigned long apicid)
55 unsigned long send_status, accept_status, start_eip;
56 int j, num_starts, maxlvt;
57 extern char _secondary_start[];
60 * Starting actual IPI sequence...
63 printk_spew("Asserting INIT.\n");
66 * Turn INIT on target chip
68 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
74 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_INT_ASSERT
77 printk_spew("Waiting for send to finish...\n");
82 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
83 } while (send_status && (timeout++ < 1000));
84 if (timeout >= 1000) {
85 printk_err("CPU %d: First apic write timed out. Disabling\n",
88 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
89 if (lapic_read(LAPIC_ESR)) {
90 printk_err("Try to reset ESR\n");
91 lapic_write_around(LAPIC_ESR, 0);
92 printk_err("ESR is 0x%x\n", lapic_read(LAPIC_ESR));
98 printk_spew("Deasserting INIT.\n");
101 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
104 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT);
106 printk_spew("Waiting for send to finish...\n");
111 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
112 } while (send_status && (timeout++ < 1000));
113 if (timeout >= 1000) {
114 printk_err("CPU %d: Second apic write timed out. Disabling\n",
120 start_eip = get_valid_start_eip((unsigned long)_secondary_start);
121 printk_debug("start_eip=0x%08lx\n", start_eip);
126 * Run STARTUP IPI loop.
128 printk_spew("#startup loops: %d.\n", num_starts);
132 for (j = 1; j <= num_starts; j++) {
133 printk_spew("Sending STARTUP #%d to %u.\n", j, apicid);
134 lapic_read_around(LAPIC_SPIV);
135 lapic_write(LAPIC_ESR, 0);
136 lapic_read(LAPIC_ESR);
137 printk_spew("After apic_write.\n");
144 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
146 /* Boot on the stack */
147 /* Kick the second */
148 lapic_write_around(LAPIC_ICR, LAPIC_DM_STARTUP
149 | (start_eip >> 12));
152 * Give the other CPU some time to accept the IPI.
156 printk_spew("Startup point 1.\n");
158 printk_spew("Waiting for send to finish...\n");
163 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
164 } while (send_status && (timeout++ < 1000));
167 * Give the other CPU some time to accept the IPI.
171 * Due to the Pentium erratum 3AP.
174 lapic_read_around(LAPIC_SPIV);
175 lapic_write(LAPIC_ESR, 0);
177 accept_status = (lapic_read(LAPIC_ESR) & 0xEF);
178 if (send_status || accept_status)
181 printk_spew("After Startup.\n");
183 printk_warning("APIC never delivered???\n");
185 printk_warning("APIC delivery error (%lx).\n", accept_status);
186 if (send_status || accept_status)
191 /* Number of cpus that are currently running in linuxbios */
192 static atomic_t active_cpus = ATOMIC_INIT(1);
194 /* start_cpu_lock covers last_cpu_index and secondary_stack.
195 * Only starting one cpu at a time let's me remove the logic
196 * for select the stack from assembly language.
198 * In addition communicating by variables to the cpu I
199 * am starting allows me to veryify it has started before
203 static spinlock_t start_cpu_lock = SPIN_LOCK_UNLOCKED;
204 static unsigned last_cpu_index = 0;
205 volatile unsigned long secondary_stack;
207 int start_cpu(device_t cpu)
209 extern unsigned char _estack[];
210 struct cpu_info *info;
211 unsigned long stack_end;
212 unsigned long apicid;
217 spin_lock(&start_cpu_lock);
219 /* Get the cpu's apicid */
220 apicid = cpu->path.u.apic.apic_id;
222 /* Get an index for the new processor */
223 index = ++last_cpu_index;
225 /* Find end of the new processors stack */
226 #if (CONFIG_LB_MEM_TOPK>1024) && (_RAMBASE < 0x100000) && ((CONFIG_CONSOLE_VGA==1) || (CONFIG_PCI_ROM_RUN == 1))
227 if(index<1) { // only keep bsp on low
228 stack_end = ((unsigned long)_estack) - (STACK_SIZE*index) - sizeof(struct cpu_info);
230 // for all APs, let use stack after pgtbl, 20480 is the pgtbl size for every cpu
231 stack_end = 0x100000+(20480 + STACK_SIZE)*CONFIG_MAX_CPUS - (STACK_SIZE*index);
232 #if (0x100000+(20480 + STACK_SIZE)*CONFIG_MAX_CPU) > (CONFIG_LB_MEM_TOPK<<10)
233 #warning "We may need to increase CONFIG_LB_MEM_TOPK, it need to be more than (0x100000+(20480 + STACK_SIZE)*CONFIG_MAX_CPU)\n"
235 if(stack_end > (CONFIG_LB_MEM_TOPK<<10)) {
236 printk_debug("start_cpu: Please increase the CONFIG_LB_MEM_TOPK more than %dK\n", stack_end>>10);
237 die("Can not go on\n");
239 stack_end -= sizeof(struct cpu_info);
242 stack_end = ((unsigned long)_estack) - (STACK_SIZE*index) - sizeof(struct cpu_info);
246 /* Record the index and which cpu structure we are using */
247 info = (struct cpu_info *)stack_end;
251 /* Advertise the new stack to start_cpu */
252 secondary_stack = stack_end;
254 /* Until the cpu starts up report the cpu is not enabled */
256 cpu->initialized = 0;
259 result = lapic_start_cpu(apicid);
263 /* Wait 1s or until the new the new cpu calls in */
264 for(count = 0; count < 100000 ; count++) {
265 if (secondary_stack == 0) {
273 spin_unlock(&start_cpu_lock);
277 /* C entry point of secondary cpus */
278 void secondary_cpu_init(void)
280 atomic_inc(&active_cpus);
281 #if SERIAL_CPU_INIT == 1
282 #if CONFIG_MAX_CPUS>2
283 spin_lock(&start_cpu_lock);
287 #if SERIAL_CPU_INIT == 1
288 #if CONFIG_MAX_CPUS>2
289 spin_unlock(&start_cpu_lock);
293 atomic_dec(&active_cpus);
297 static void initialize_other_cpus(struct bus *cpu_bus)
299 int old_active_count, active_count;
301 /* Loop through the cpus once getting them started */
303 for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) {
304 if (cpu->path.type != DEVICE_PATH_APIC) {
312 if (cpu->initialized) {
316 if (!start_cpu(cpu)) {
317 /* Record the error in cpu? */
318 printk_err("CPU %u would not start!\n",
319 cpu->path.u.apic.apic_id);
321 #if SERIAL_CPU_INIT == 1
322 #if CONFIG_MAX_CPUS>2
328 /* Now loop until the other cpus have finished initializing */
329 old_active_count = 1;
330 active_count = atomic_read(&active_cpus);
331 while(active_count > 1) {
332 if (active_count != old_active_count) {
333 printk_info("Waiting for %d CPUS to stop\n", active_count - 1);
334 old_active_count = active_count;
337 active_count = atomic_read(&active_cpus);
339 for(cpu = cpu_bus->children; cpu; cpu = cpu->sibling) {
340 if (cpu->path.type != DEVICE_PATH_APIC) {
343 if (!cpu->initialized) {
344 printk_err("CPU %u did not initialize!\n",
345 cpu->path.u.apic.apic_id);
346 #warning "FIXME do I need a mainboard_cpu_fixup function?"
349 printk_debug("All AP CPUs stopped\n");
352 #else /* CONFIG_SMP */
353 #define initialize_other_cpus(root) do {} while(0)
354 #endif /* CONFIG_SMP */
356 void initialize_cpus(struct bus *cpu_bus)
358 struct device_path cpu_path;
359 struct cpu_info *info;
361 /* Find the info struct for this cpu */
365 /* Ensure the local apic is enabled */
368 /* Get the device path of the boot cpu */
369 cpu_path.type = DEVICE_PATH_APIC;
370 cpu_path.u.apic.apic_id = lapicid();
372 /* Get the device path of the boot cpu */
373 cpu_path.type = DEVICE_PATH_CPU;
374 cpu_path.u.cpu.id = 0;
377 /* Find the device structure for the boot cpu */
378 info->cpu = alloc_find_dev(cpu_bus, &cpu_path);
380 copy_secondary_start_to_1m_below(); // why here? In case some day we can start core1 in amd_sibling_init
382 /* Initialize the bootstrap processor */
385 /* Now initialize the rest of the cpus */
386 initialize_other_cpus(cpu_bus);