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
13 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
14 #define APIC_SVR ((u8*)BUILD_APIC_ADDR + 0x0F0)
15 #define APIC_LINT0 ((u8*)BUILD_APIC_ADDR + 0x350)
16 #define APIC_LINT1 ((u8*)BUILD_APIC_ADDR + 0x360)
18 #define APIC_ENABLED 0x0100
20 struct { u32 ecx, eax, edx; } smp_mtrr[32] VAR16VISIBLE;
21 u32 smp_mtrr_count VAR16VISIBLE;
24 wrmsr_smp(u32 index, u64 val)
27 if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr)) {
31 smp_mtrr[smp_mtrr_count].ecx = index;
32 smp_mtrr[smp_mtrr_count].eax = val;
33 smp_mtrr[smp_mtrr_count].edx = val >> 32;
37 u32 CountCPUs VAR16VISIBLE;
38 u32 MaxCountCPUs VAR16VISIBLE;
39 extern void smp_ap_boot_code(void);
41 " .global smp_ap_boot_code\n"
45 " movw $" __stringify(SEG_BIOS) ", %ax\n"
49 " movl $smp_mtrr, %esi\n"
50 " movl smp_mtrr_count, %ebx\n"
51 "1:testl %ebx, %ebx\n"
53 " movl 0(%esi), %ecx\n"
54 " movl 4(%esi), %eax\n"
55 " movl 8(%esi), %edx\n"
62 // Increment the cpu counter
63 " lock incl CountCPUs\n"
65 // Halt the processor.
70 // find and initialize the CPUs by launching a SIPI to them
75 u32 eax, ebx, ecx, cpuid_features;
76 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
77 if (eax < 1 || !(cpuid_features & CPUID_APIC)) {
78 // No apic - only the main cpu is present.
79 dprintf(1, "No apic - only the main cpu is present.\n");
86 writel(&CountCPUs, 1);
88 // Setup jump trampoline to counter code.
89 u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
90 // ljmpw $SEG_BIOS, $(smp_ap_boot_code - BUILD_BIOS_ADDR)
91 u64 new = (0xea | ((u64)SEG_BIOS<<24)
92 | (((u32)smp_ap_boot_code - BUILD_BIOS_ADDR) << 8));
93 *(u64*)BUILD_AP_BOOT_ADDR = new;
96 u32 val = readl(APIC_SVR);
97 writel(APIC_SVR, val | APIC_ENABLED);
99 if (! CONFIG_COREBOOT) {
100 /* Set LINT0 as Ext_INT, level triggered */
101 writel(APIC_LINT0, 0x8700);
103 /* Set LINT1 as NMI, level triggered */
104 writel(APIC_LINT1, 0x8400);
109 writel(APIC_ICR_LOW, 0x000C4500);
110 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
111 writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
113 // Wait for other CPUs to process the SIPI.
114 if (CONFIG_COREBOOT) {
117 u8 cmos_smp_count = inb_cmos(CMOS_BIOS_SMP_COUNT);
118 while (cmos_smp_count + 1 != readl(&CountCPUs))
123 *(u64*)BUILD_AP_BOOT_ADDR = old;
125 MaxCountCPUs = qemu_cfg_get_max_cpus();
126 if (!MaxCountCPUs || MaxCountCPUs < CountCPUs)
127 MaxCountCPUs = CountCPUs;
129 dprintf(1, "Found %d cpu(s) max supported %d cpu(s)\n", readl(&CountCPUs),