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;
52 ".global smp_ap_boot_code_start\n"
53 ".global smp_ap_boot_code_end\n"
56 "smp_ap_boot_code_start:\n"
57 // Increament the counter at BUILD_CPU_COUNT_ADDR
60 " lock incw " __stringify(BUILD_CPU_COUNT_ADDR) "\n"
61 // Halt the processor.
62 " ljmpl $" __stringify(SEG_BIOS) ", $(permanent_halt - " __stringify(BUILD_BIOS_ADDR) ")\n"
63 "smp_ap_boot_code_end:\n"
68 extern u8 smp_ap_boot_code_start;
69 extern u8 smp_ap_boot_code_end;
73 /* find the number of CPUs by launching a SIPI to them */
82 u32 eax, ebx, ecx, cpuid_features;
83 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
84 if (cpuid_features & CPUID_APIC) {
85 /* enable local APIC */
86 u32 val = readl(APIC_BASE + APIC_SVR);
88 writel(APIC_BASE + APIC_SVR, val);
90 writew((void *)BUILD_CPU_COUNT_ADDR, 1);
91 /* copy AP boot code */
92 memcpy((void *)BUILD_AP_BOOT_ADDR, &smp_ap_boot_code_start,
93 &smp_ap_boot_code_end - &smp_ap_boot_code_start);
96 writel(APIC_BASE + APIC_ICR_LOW, 0x000C4500);
97 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
98 writel(APIC_BASE + APIC_ICR_LOW, 0x000C4600 | sipi_vector);
102 smp_cpus = readw((void *)BUILD_CPU_COUNT_ADDR);
104 dprintf(1, "Found %d cpu(s)\n", smp_cpus);