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 GPLv3 license.
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
11 #define CPUID_APIC (1 << 9)
13 #define APIC_BASE ((u8 *)0xfee00000)
14 #define APIC_ICR_LOW 0x300
15 #define APIC_SVR 0x0F0
17 #define APIC_LVT3 0x370
19 #define APIC_ENABLED 0x0100
21 static inline void writel(void *addr, u32 val)
23 *(volatile u32 *)addr = val;
26 static inline void writew(void *addr, u16 val)
28 *(volatile u16 *)addr = val;
31 static inline void writeb(void *addr, u8 val)
33 *(volatile u8 *)addr = val;
36 static inline u32 readl(const void *addr)
38 return *(volatile const u32 *)addr;
41 static inline u16 readw(const void *addr)
43 return *(volatile const u16 *)addr;
46 static inline u8 readb(const void *addr)
48 return *(volatile const u8 *)addr;
51 extern void smp_ap_boot_code();
54 u32 smp_cpus VISIBLE16;
56 " .global smp_ap_boot_code\n"
58 // Increament the cpu counter
59 " movw $" __stringify(SEG_BIOS) ", %ax\n"
61 " lock incl smp_cpus\n"
62 // Halt the processor.
63 " jmp permanent_halt\n"
67 /* find the number of CPUs by launching a SIPI to them */
74 u32 eax, ebx, ecx, cpuid_features;
75 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
76 if (! (cpuid_features & CPUID_APIC)) {
77 // No apic - only the main cpu is present.
85 // Setup jump trampoline to counter code.
86 u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
87 // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
88 u64 new = (0xea | ((u64)SEG_BIOS<<24)
89 | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
90 *(u64*)BUILD_AP_BOOT_ADDR = new;
93 u32 val = readl(APIC_BASE + APIC_SVR);
94 writel(APIC_BASE + APIC_SVR, val | APIC_ENABLED);
97 writel(APIC_BASE + APIC_ICR_LOW, 0x000C4500);
98 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
99 writel(APIC_BASE + APIC_ICR_LOW, 0x000C4600 | sipi_vector);
101 // Wait for other CPUs to process the SIPI.
105 writel(APIC_BASE + APIC_SVR, val);
106 *(u64*)BUILD_AP_BOOT_ADDR = old;
108 u32 count = readl(&smp_cpus);
109 dprintf(1, "Found %d cpu(s)\n", count);
113 // Reset smp_cpus to zero (forces a recheck on reboots).
115 smp_probe_setup(void)