blob: a912857eab6e57f17ef2df5e0a673574786dfcbc [file] [log] [blame]
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04001// CPU count detection
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04007
8#include "util.h" // dprintf
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "config.h" // CONFIG_*
Kevin O'Connor31bfad62008-12-16 23:50:52 -050010#include "cmos.h" // CMOS_BIOS_SMP_COUNT
Kevin O'Connora50ec8d2009-06-15 23:16:15 -040011#include "farptr.h" // ASSERT32
Kevin O'Connor84705852009-10-08 22:13:15 -040012#include "paravirt.h"
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040013
Kevin O'Connorf5c11612008-12-14 10:11:45 -050014#define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
15#define APIC_SVR ((u8*)BUILD_APIC_ADDR + 0x0F0)
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040016
17#define APIC_ENABLED 0x0100
18
Kevin O'Connor372e0712009-09-09 09:51:31 -040019struct { u32 ecx, eax, edx; } smp_mtrr[16] VAR16VISIBLE;
20u32 smp_mtrr_count VAR16VISIBLE;
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040021
22void
23wrmsr_smp(u32 index, u64 val)
24{
25 wrmsr(index, val);
26 if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr))
27 return;
28 smp_mtrr[smp_mtrr_count].ecx = index;
29 smp_mtrr[smp_mtrr_count].eax = val;
30 smp_mtrr[smp_mtrr_count].edx = val >> 32;
31 smp_mtrr_count++;
32}
33
Kevin O'Connor372e0712009-09-09 09:51:31 -040034u32 CountCPUs VAR16VISIBLE;
Kevin O'Connor84705852009-10-08 22:13:15 -040035u32 MaxCountCPUs VAR16VISIBLE;
Kevin O'Connora06bfb62008-12-06 19:37:56 -050036extern void smp_ap_boot_code();
Kevin O'Connor4a754b32008-12-28 21:37:27 -050037ASM16(
Kevin O'Connora06bfb62008-12-06 19:37:56 -050038 " .global smp_ap_boot_code\n"
39 "smp_ap_boot_code:\n"
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040040
41 // Setup data segment
Kevin O'Connora06bfb62008-12-06 19:37:56 -050042 " movw $" __stringify(SEG_BIOS) ", %ax\n"
Kevin O'Connor484270d2008-08-17 10:50:57 -040043 " movw %ax, %ds\n"
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040044
45 // MTRR setup
46 " movl $smp_mtrr, %esi\n"
47 " movl smp_mtrr_count, %ebx\n"
48 "1:testl %ebx, %ebx\n"
49 " jz 2f\n"
50 " movl 0(%esi), %ecx\n"
51 " movl 4(%esi), %eax\n"
52 " movl 8(%esi), %edx\n"
53 " wrmsr\n"
54 " addl $12, %esi\n"
55 " decl %ebx\n"
56 " jmp 1b\n"
57 "2:\n"
58
59 // Increment the cpu counter
60 " lock incl CountCPUs\n"
61
Kevin O'Connor484270d2008-08-17 10:50:57 -040062 // Halt the processor.
Kevin O'Connor60b69992009-02-07 13:25:25 -050063 "1:hlt\n"
64 " jmp 1b\n"
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040065 );
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040066
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040067// find and initialize the CPUs by launching a SIPI to them
68void
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040069smp_probe(void)
70{
Kevin O'Connor0b60a062009-06-15 23:03:05 -040071 ASSERT32();
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040072 u32 eax, ebx, ecx, cpuid_features;
73 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050074 if (! (cpuid_features & CPUID_APIC)) {
75 // No apic - only the main cpu is present.
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040076 CountCPUs= 1;
77 return;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040078 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040079
Kevin O'Connora06bfb62008-12-06 19:37:56 -050080 // Init the counter.
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040081 writel(&CountCPUs, 1);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050082
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
Kevin O'Connorf5c11612008-12-14 10:11:45 -050091 u32 val = readl(APIC_SVR);
92 writel(APIC_SVR, val | APIC_ENABLED);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050093
94 // broadcast SIPI
Kevin O'Connorf5c11612008-12-14 10:11:45 -050095 writel(APIC_ICR_LOW, 0x000C4500);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050096 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
Kevin O'Connorf5c11612008-12-14 10:11:45 -050097 writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050098
99 // Wait for other CPUs to process the SIPI.
Kevin O'Connor31bfad62008-12-16 23:50:52 -0500100 if (CONFIG_COREBOOT)
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400101 msleep(10);
Kevin O'Connor31bfad62008-12-16 23:50:52 -0500102 else
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400103 while (inb_cmos(CMOS_BIOS_SMP_COUNT) + 1 != readl(&CountCPUs))
Kevin O'Connor31bfad62008-12-16 23:50:52 -0500104 ;
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500105
106 // Restore memory.
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500107 *(u64*)BUILD_AP_BOOT_ADDR = old;
108
Kevin O'Connor84705852009-10-08 22:13:15 -0400109 MaxCountCPUs = qemu_cfg_get_max_cpus();
110 if (!MaxCountCPUs || MaxCountCPUs < CountCPUs)
111 MaxCountCPUs = CountCPUs;
112
113 dprintf(1, "Found %d cpu(s) max supported %d cpu(s)\n", readl(&CountCPUs),
114 MaxCountCPUs);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400115}
Kevin O'Connoracf13742008-11-29 11:19:19 -0500116
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400117// Reset variables to zero
Kevin O'Connoracf13742008-11-29 11:19:19 -0500118void
119smp_probe_setup(void)
120{
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400121 CountCPUs = 0;
122 smp_mtrr_count = 0;
Kevin O'Connoracf13742008-11-29 11:19:19 -0500123}