blob: 8c077a1bab49e2a80b810ab1eb673a5291df5e23 [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'Connor84705852009-10-08 22:13:15 -040011#include "paravirt.h"
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040012
Kevin O'Connorf5c11612008-12-14 10:11:45 -050013#define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
14#define APIC_SVR ((u8*)BUILD_APIC_ADDR + 0x0F0)
Kevin O'Connor19c1a762009-11-14 13:43:01 -050015#define APIC_LINT0 ((u8*)BUILD_APIC_ADDR + 0x350)
16#define APIC_LINT1 ((u8*)BUILD_APIC_ADDR + 0x360)
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040017
18#define APIC_ENABLED 0x0100
19
Marcelo Tosattibbce6d62011-07-29 19:40:51 -030020struct { u32 ecx, eax, edx; } smp_mtrr[32] VAR16VISIBLE;
Kevin O'Connor372e0712009-09-09 09:51:31 -040021u32 smp_mtrr_count VAR16VISIBLE;
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040022
23void
24wrmsr_smp(u32 index, u64 val)
25{
26 wrmsr(index, val);
Kevin O'Connore0f87ce2011-07-29 19:21:07 -040027 if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr)) {
28 warn_noalloc();
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040029 return;
Kevin O'Connore0f87ce2011-07-29 19:21:07 -040030 }
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040031 smp_mtrr[smp_mtrr_count].ecx = index;
32 smp_mtrr[smp_mtrr_count].eax = val;
33 smp_mtrr[smp_mtrr_count].edx = val >> 32;
34 smp_mtrr_count++;
35}
36
Kevin O'Connor372e0712009-09-09 09:51:31 -040037u32 CountCPUs VAR16VISIBLE;
Kevin O'Connor84705852009-10-08 22:13:15 -040038u32 MaxCountCPUs VAR16VISIBLE;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050039extern void smp_ap_boot_code(void);
Kevin O'Connor4a754b32008-12-28 21:37:27 -050040ASM16(
Kevin O'Connora06bfb62008-12-06 19:37:56 -050041 " .global smp_ap_boot_code\n"
42 "smp_ap_boot_code:\n"
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040043
44 // Setup data segment
Kevin O'Connora06bfb62008-12-06 19:37:56 -050045 " movw $" __stringify(SEG_BIOS) ", %ax\n"
Kevin O'Connor484270d2008-08-17 10:50:57 -040046 " movw %ax, %ds\n"
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040047
48 // MTRR setup
49 " movl $smp_mtrr, %esi\n"
50 " movl smp_mtrr_count, %ebx\n"
51 "1:testl %ebx, %ebx\n"
52 " jz 2f\n"
53 " movl 0(%esi), %ecx\n"
54 " movl 4(%esi), %eax\n"
55 " movl 8(%esi), %edx\n"
56 " wrmsr\n"
57 " addl $12, %esi\n"
58 " decl %ebx\n"
59 " jmp 1b\n"
60 "2:\n"
61
62 // Increment the cpu counter
63 " lock incl CountCPUs\n"
64
Kevin O'Connor484270d2008-08-17 10:50:57 -040065 // Halt the processor.
Kevin O'Connor60b69992009-02-07 13:25:25 -050066 "1:hlt\n"
67 " jmp 1b\n"
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040068 );
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040069
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040070// find and initialize the CPUs by launching a SIPI to them
71void
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040072smp_probe(void)
73{
Kevin O'Connor52a300f2009-12-26 23:32:57 -050074 ASSERT32FLAT();
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040075 u32 eax, ebx, ecx, cpuid_features;
76 cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
Lubomir Rintel22f63782010-08-20 13:37:54 +020077 if (eax < 1 || !(cpuid_features & CPUID_APIC)) {
Kevin O'Connora06bfb62008-12-06 19:37:56 -050078 // No apic - only the main cpu is present.
Kevin O'Connorb49e1e32009-11-24 09:41:06 -050079 dprintf(1, "No apic - only the main cpu is present.\n");
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040080 CountCPUs= 1;
Kevin O'Connorb49e1e32009-11-24 09:41:06 -050081 MaxCountCPUs = 1;
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040082 return;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040083 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040084
Kevin O'Connora06bfb62008-12-06 19:37:56 -050085 // Init the counter.
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040086 writel(&CountCPUs, 1);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050087
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;
94
95 // enable local APIC
Kevin O'Connorf5c11612008-12-14 10:11:45 -050096 u32 val = readl(APIC_SVR);
97 writel(APIC_SVR, val | APIC_ENABLED);
Kevin O'Connora06bfb62008-12-06 19:37:56 -050098
Kevin O'Connor19c1a762009-11-14 13:43:01 -050099 if (! CONFIG_COREBOOT) {
100 /* Set LINT0 as Ext_INT, level triggered */
101 writel(APIC_LINT0, 0x8700);
102
103 /* Set LINT1 as NMI, level triggered */
104 writel(APIC_LINT1, 0x8400);
105 }
106
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500107 // broadcast SIPI
Kevin O'Connor808939c2010-03-10 22:32:26 -0500108 barrier();
Kevin O'Connorf5c11612008-12-14 10:11:45 -0500109 writel(APIC_ICR_LOW, 0x000C4500);
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500110 u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
Kevin O'Connorf5c11612008-12-14 10:11:45 -0500111 writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500112
113 // Wait for other CPUs to process the SIPI.
Kevin O'Connordb981072009-11-09 19:21:44 -0500114 if (CONFIG_COREBOOT) {
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400115 msleep(10);
Kevin O'Connordb981072009-11-09 19:21:44 -0500116 } else {
117 u8 cmos_smp_count = inb_cmos(CMOS_BIOS_SMP_COUNT);
118 while (cmos_smp_count + 1 != readl(&CountCPUs))
Kevin O'Connor808939c2010-03-10 22:32:26 -0500119 yield();
Kevin O'Connordb981072009-11-09 19:21:44 -0500120 }
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500121
122 // Restore memory.
Kevin O'Connora06bfb62008-12-06 19:37:56 -0500123 *(u64*)BUILD_AP_BOOT_ADDR = old;
124
Kevin O'Connor84705852009-10-08 22:13:15 -0400125 MaxCountCPUs = qemu_cfg_get_max_cpus();
126 if (!MaxCountCPUs || MaxCountCPUs < CountCPUs)
127 MaxCountCPUs = CountCPUs;
128
129 dprintf(1, "Found %d cpu(s) max supported %d cpu(s)\n", readl(&CountCPUs),
130 MaxCountCPUs);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400131}