3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_BIOS_SMP_COUNT
12 #define CPUID_APIC (1 << 9)
14 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
15 #define APIC_SVR ((u8*)BUILD_APIC_ADDR + 0x0F0)
17 #define APIC_ENABLED 0x0100
19 static inline void writel(void *addr, u32 val)
21 *(volatile u32 *)addr = val;
24 static inline void writew(void *addr, u16 val)
26 *(volatile u16 *)addr = val;
29 static inline void writeb(void *addr, u8 val)
31 *(volatile u8 *)addr = val;
34 static inline u32 readl(const void *addr)
36 return *(volatile const u32 *)addr;
39 static inline u16 readw(const void *addr)
41 return *(volatile const u16 *)addr;
44 static inline u8 readb(const void *addr)
46 return *(volatile const u8 *)addr;
49 u32 smp_cpus VAR16_32;
50 extern void smp_ap_boot_code();
52 " .global smp_ap_boot_code\n"
54 // Increment the cpu counter
55 " movw $" __stringify(SEG_BIOS) ", %ax\n"
57 " lock incl smp_cpus\n"
58 // Halt the processor.
59 " jmp permanent_halt\n"
62 /* find the number of CPUs by launching a SIPI to them */
69 u32 eax, ebx, ecx, cpuid_features;
70 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
71 if (! (cpuid_features & CPUID_APIC)) {
72 // No apic - only the main cpu is present.
80 // Setup jump trampoline to counter code.
81 u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
82 // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
83 u64 new = (0xea | ((u64)SEG_BIOS<<24)
84 | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
85 *(u64*)BUILD_AP_BOOT_ADDR = new;
88 u32 val = readl(APIC_SVR);
89 writel(APIC_SVR, val | APIC_ENABLED);
92 writel(APIC_ICR_LOW, 0x000C4500);
93 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
94 writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
96 // Wait for other CPUs to process the SIPI.
100 while (inb_cmos(CMOS_BIOS_SMP_COUNT) + 1 != readl(&smp_cpus))
104 *(u64*)BUILD_AP_BOOT_ADDR = old;
106 u32 count = readl(&smp_cpus);
107 dprintf(1, "Found %d cpu(s)\n", count);
111 // Reset smp_cpus to zero (forces a recheck on reboots).
113 smp_probe_setup(void)