Support running non-pci specific vga option roms from CBFS.
[seabios.git] / src / smpdetect.c
1 // CPU count detection
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_BIOS_SMP_COUNT
11 #include "farptr.h" // ASSERT32
12
13 #define CPUID_APIC (1 << 9)
14
15 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
16 #define APIC_SVR     ((u8*)BUILD_APIC_ADDR + 0x0F0)
17
18 #define APIC_ENABLED 0x0100
19
20 static inline void writel(void *addr, u32 val)
21 {
22     *(volatile u32 *)addr = val;
23 }
24
25 static inline void writew(void *addr, u16 val)
26 {
27     *(volatile u16 *)addr = val;
28 }
29
30 static inline void writeb(void *addr, u8 val)
31 {
32     *(volatile u8 *)addr = val;
33 }
34
35 static inline u32 readl(const void *addr)
36 {
37     return *(volatile const u32 *)addr;
38 }
39
40 static inline u16 readw(const void *addr)
41 {
42     return *(volatile const u16 *)addr;
43 }
44
45 static inline u8 readb(const void *addr)
46 {
47     return *(volatile const u8 *)addr;
48 }
49
50 u32 smp_cpus VAR16_32;
51 extern void smp_ap_boot_code();
52 ASM16(
53     "  .global smp_ap_boot_code\n"
54     "smp_ap_boot_code:\n"
55     // Increment the cpu counter
56     "  movw $" __stringify(SEG_BIOS) ", %ax\n"
57     "  movw %ax, %ds\n"
58     "  lock incl smp_cpus\n"
59     // Halt the processor.
60     "1:hlt\n"
61     "  jmp 1b\n"
62     );
63
64 /* find the number of CPUs by launching a SIPI to them */
65 int
66 smp_probe(void)
67 {
68     ASSERT32();
69     if (smp_cpus)
70         return smp_cpus;
71
72     u32 eax, ebx, ecx, cpuid_features;
73     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
74     if (! (cpuid_features & CPUID_APIC)) {
75         // No apic - only the main cpu is present.
76         smp_cpus = 1;
77         return 1;
78     }
79
80     // Init the counter.
81     writel(&smp_cpus, 1);
82
83     // Setup jump trampoline to counter code.
84     u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
85     // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
86     u64 new = (0xea | ((u64)SEG_BIOS<<24)
87                | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
88     *(u64*)BUILD_AP_BOOT_ADDR = new;
89
90     // enable local APIC
91     u32 val = readl(APIC_SVR);
92     writel(APIC_SVR, val | APIC_ENABLED);
93
94     // broadcast SIPI
95     writel(APIC_ICR_LOW, 0x000C4500);
96     u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
97     writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
98
99     // Wait for other CPUs to process the SIPI.
100     if (CONFIG_COREBOOT)
101         mdelay(10);
102     else
103         while (inb_cmos(CMOS_BIOS_SMP_COUNT) + 1 != readl(&smp_cpus))
104             ;
105
106     // Restore memory.
107     *(u64*)BUILD_AP_BOOT_ADDR = old;
108
109     u32 count = readl(&smp_cpus);
110     dprintf(1, "Found %d cpu(s)\n", count);
111     return count;
112 }
113
114 // Reset smp_cpus to zero (forces a recheck on reboots).
115 void
116 smp_probe_setup(void)
117 {
118     smp_cpus = 0;
119 }