Rename VAR16_32 to VAR16VISIBLE.
[seabios.git] / src / smp.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 APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
14 #define APIC_SVR     ((u8*)BUILD_APIC_ADDR + 0x0F0)
15
16 #define APIC_ENABLED 0x0100
17
18 static inline void writel(void *addr, u32 val)
19 {
20     *(volatile u32 *)addr = val;
21 }
22
23 static inline void writew(void *addr, u16 val)
24 {
25     *(volatile u16 *)addr = val;
26 }
27
28 static inline void writeb(void *addr, u8 val)
29 {
30     *(volatile u8 *)addr = val;
31 }
32
33 static inline u32 readl(const void *addr)
34 {
35     return *(volatile const u32 *)addr;
36 }
37
38 static inline u16 readw(const void *addr)
39 {
40     return *(volatile const u16 *)addr;
41 }
42
43 static inline u8 readb(const void *addr)
44 {
45     return *(volatile const u8 *)addr;
46 }
47
48 struct { u32 ecx, eax, edx; } smp_mtrr[16] VAR16VISIBLE;
49 u32 smp_mtrr_count VAR16VISIBLE;
50
51 void
52 wrmsr_smp(u32 index, u64 val)
53 {
54     wrmsr(index, val);
55     if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr))
56         return;
57     smp_mtrr[smp_mtrr_count].ecx = index;
58     smp_mtrr[smp_mtrr_count].eax = val;
59     smp_mtrr[smp_mtrr_count].edx = val >> 32;
60     smp_mtrr_count++;
61 }
62
63 u32 CountCPUs VAR16VISIBLE;
64 extern void smp_ap_boot_code();
65 ASM16(
66     "  .global smp_ap_boot_code\n"
67     "smp_ap_boot_code:\n"
68
69     // Setup data segment
70     "  movw $" __stringify(SEG_BIOS) ", %ax\n"
71     "  movw %ax, %ds\n"
72
73     // MTRR setup
74     "  movl $smp_mtrr, %esi\n"
75     "  movl smp_mtrr_count, %ebx\n"
76     "1:testl %ebx, %ebx\n"
77     "  jz 2f\n"
78     "  movl 0(%esi), %ecx\n"
79     "  movl 4(%esi), %eax\n"
80     "  movl 8(%esi), %edx\n"
81     "  wrmsr\n"
82     "  addl $12, %esi\n"
83     "  decl %ebx\n"
84     "  jmp 1b\n"
85     "2:\n"
86
87     // Increment the cpu counter
88     "  lock incl CountCPUs\n"
89
90     // Halt the processor.
91     "1:hlt\n"
92     "  jmp 1b\n"
93     );
94
95 // find and initialize the CPUs by launching a SIPI to them
96 void
97 smp_probe(void)
98 {
99     ASSERT32();
100     u32 eax, ebx, ecx, cpuid_features;
101     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
102     if (! (cpuid_features & CPUID_APIC)) {
103         // No apic - only the main cpu is present.
104         CountCPUs= 1;
105         return;
106     }
107
108     // Init the counter.
109     writel(&CountCPUs, 1);
110
111     // Setup jump trampoline to counter code.
112     u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
113     // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
114     u64 new = (0xea | ((u64)SEG_BIOS<<24)
115                | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
116     *(u64*)BUILD_AP_BOOT_ADDR = new;
117
118     // enable local APIC
119     u32 val = readl(APIC_SVR);
120     writel(APIC_SVR, val | APIC_ENABLED);
121
122     // broadcast SIPI
123     writel(APIC_ICR_LOW, 0x000C4500);
124     u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
125     writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
126
127     // Wait for other CPUs to process the SIPI.
128     if (CONFIG_COREBOOT)
129         mdelay(10);
130     else
131         while (inb_cmos(CMOS_BIOS_SMP_COUNT) + 1 != readl(&CountCPUs))
132             ;
133
134     // Restore memory.
135     *(u64*)BUILD_AP_BOOT_ADDR = old;
136
137     dprintf(1, "Found %d cpu(s)\n", readl(&CountCPUs));
138 }
139
140 // Reset variables to zero
141 void
142 smp_probe_setup(void)
143 {
144     CountCPUs = 0;
145     smp_mtrr_count = 0;
146 }