Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / fw / smp.c
1 // QEMU multi-CPU initialization code
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 "config.h" // CONFIG_*
9 #include "hw/rtc.h" // CMOS_BIOS_SMP_COUNT
10 #include "output.h" // dprintf
11 #include "romfile.h" // romfile_loadint
12 #include "stacks.h" // yield
13 #include "util.h" // smp_setup
14 #include "x86.h" // wrmsr
15
16 #define APIC_ICR_LOW ((u8*)BUILD_APIC_ADDR + 0x300)
17 #define APIC_SVR     ((u8*)BUILD_APIC_ADDR + 0x0F0)
18 #define APIC_LINT0   ((u8*)BUILD_APIC_ADDR + 0x350)
19 #define APIC_LINT1   ((u8*)BUILD_APIC_ADDR + 0x360)
20
21 #define APIC_ENABLED 0x0100
22
23 static struct { u32 index; u64 val; } smp_mtrr[32];
24 static u32 smp_mtrr_count;
25
26 void
27 wrmsr_smp(u32 index, u64 val)
28 {
29     wrmsr(index, val);
30     if (smp_mtrr_count >= ARRAY_SIZE(smp_mtrr)) {
31         warn_noalloc();
32         return;
33     }
34     smp_mtrr[smp_mtrr_count].index = index;
35     smp_mtrr[smp_mtrr_count].val = val;
36     smp_mtrr_count++;
37 }
38
39 u32 MaxCountCPUs;
40 static u32 CountCPUs;
41 // 256 bits for the found APIC IDs
42 static u32 FoundAPICIDs[256/32];
43
44 int apic_id_is_present(u8 apic_id)
45 {
46     return !!(FoundAPICIDs[apic_id/32] & (1ul << (apic_id % 32)));
47 }
48
49 void VISIBLE32FLAT
50 handle_smp(void)
51 {
52     if (!CONFIG_QEMU)
53         return;
54
55     // Enable CPU caching
56     setcr0(getcr0() & ~(CR0_CD|CR0_NW));
57
58     // Detect apic_id
59     u32 eax, ebx, ecx, cpuid_features;
60     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
61     u8 apic_id = ebx>>24;
62     dprintf(DEBUG_HDL_smp, "handle_smp: apic_id=%d\n", apic_id);
63
64     // MTRR setup
65     int i;
66     for (i=0; i<smp_mtrr_count; i++)
67         wrmsr(smp_mtrr[i].index, smp_mtrr[i].val);
68
69     // Set bit on FoundAPICIDs
70     FoundAPICIDs[apic_id/32] |= (1 << (apic_id % 32));
71
72     CountCPUs++;
73 }
74
75 // Atomic lock for shared stack across processors.
76 u32 SMPLock __VISIBLE;
77 u32 SMPStack __VISIBLE;
78
79 // find and initialize the CPUs by launching a SIPI to them
80 void
81 smp_setup(void)
82 {
83     if (!CONFIG_QEMU)
84         return;
85
86     ASSERT32FLAT();
87     u32 eax, ebx, ecx, cpuid_features;
88     cpuid(1, &eax, &ebx, &ecx, &cpuid_features);
89     if (eax < 1 || !(cpuid_features & CPUID_APIC)) {
90         // No apic - only the main cpu is present.
91         dprintf(1, "No apic - only the main cpu is present.\n");
92         CountCPUs= 1;
93         MaxCountCPUs = 1;
94         return;
95     }
96
97     // mark the BSP initial APIC ID as found, too:
98     u8 apic_id = ebx>>24;
99     FoundAPICIDs[apic_id/32] |= (1 << (apic_id % 32));
100     CountCPUs = 1;
101
102     // Setup jump trampoline to counter code.
103     u64 old = *(u64*)BUILD_AP_BOOT_ADDR;
104     // ljmpw $SEG_BIOS, $(entry_smp - BUILD_BIOS_ADDR)
105     extern void entry_smp(void);
106     u64 new = (0xea | ((u64)SEG_BIOS<<24)
107                | (((u32)entry_smp - BUILD_BIOS_ADDR) << 8));
108     *(u64*)BUILD_AP_BOOT_ADDR = new;
109
110     // enable local APIC
111     u32 val = readl(APIC_SVR);
112     writel(APIC_SVR, val | APIC_ENABLED);
113
114     /* Set LINT0 as Ext_INT, level triggered */
115     writel(APIC_LINT0, 0x8700);
116
117     /* Set LINT1 as NMI, level triggered */
118     writel(APIC_LINT1, 0x8400);
119
120     // Init the lock.
121     writel(&SMPLock, 1);
122
123     // broadcast SIPI
124     barrier();
125     writel(APIC_ICR_LOW, 0x000C4500);
126     u32 sipi_vector = BUILD_AP_BOOT_ADDR >> 12;
127     writel(APIC_ICR_LOW, 0x000C4600 | sipi_vector);
128
129     // Wait for other CPUs to process the SIPI.
130     u8 cmos_smp_count = rtc_read(CMOS_BIOS_SMP_COUNT) + 1;
131     while (cmos_smp_count != CountCPUs)
132         asm volatile(
133             // Release lock and allow other processors to use the stack.
134             "  movl %%esp, %1\n"
135             "  movl $0, %0\n"
136             // Reacquire lock and take back ownership of stack.
137             "1:rep ; nop\n"
138             "  lock btsl $0, %0\n"
139             "  jc 1b\n"
140             : "+m" (SMPLock), "+m" (SMPStack)
141             : : "cc", "memory");
142     yield();
143
144     // Restore memory.
145     *(u64*)BUILD_AP_BOOT_ADDR = old;
146
147     MaxCountCPUs = romfile_loadint("etc/max-cpus", 0);
148     if (!MaxCountCPUs || MaxCountCPUs < CountCPUs)
149         MaxCountCPUs = CountCPUs;
150
151     dprintf(1, "Found %d cpu(s) max supported %d cpu(s)\n", CountCPUs,
152             MaxCountCPUs);
153 }