Only add the first logical CPU in each physical CPU to the MPS tables.
[seabios.git] / src / mptable.c
1 // MPTable generation (on emulators)
2 //
3 // Copyright (C) 2008,2009  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 "mptable.h" // MPTABLE_SIGNATURE
11 #include "paravirt.h"
12
13 static inline unsigned long
14 fls(unsigned long word)
15 {
16     asm("bsr %1,%0"
17         : "=r" (word)
18         : "rm" (word));
19     return word + 1;
20 }
21
22 void
23 mptable_init(void)
24 {
25     if (! CONFIG_MPTABLE)
26         return;
27
28     dprintf(3, "init MPTable\n");
29
30     int length = (sizeof(struct mptable_config_s)
31                   + sizeof(struct mpt_cpu) * MaxCountCPUs
32                   + sizeof(struct mpt_bus)
33                   + sizeof(struct mpt_ioapic)
34                   + sizeof(struct mpt_intsrc) * 16);
35     struct mptable_config_s *config = malloc_fseg(length);
36     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
37     if (!config || !floating) {
38         dprintf(1, "No room for MPTABLE!\n");
39         return;
40     }
41
42     /* floating pointer structure */
43     memset(floating, 0, sizeof(*floating));
44     floating->signature = MPTABLE_SIGNATURE;
45     floating->physaddr = (u32)config;
46     floating->length = 1;
47     floating->spec_rev = 4;
48     floating->checksum -= checksum(floating, sizeof(*floating));
49
50     // Config structure.
51     memset(config, 0, sizeof(*config));
52     config->signature = MPCONFIG_SIGNATURE;
53     config->spec = 4;
54     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
55     memcpy(config->productid, "0.1         ", sizeof(config->productid));
56     config->lapic = BUILD_APIC_ADDR;
57
58     // CPU definitions.
59     u32 cpuid_signature, ebx, ecx, cpuid_features;
60     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
61     struct mpt_cpu *cpus = (void*)&config[1];
62     int i, actual_cpu_count;
63     for (i = 0, actual_cpu_count = 0; i < MaxCountCPUs; i++) {
64         struct mpt_cpu *cpu = &cpus[i];
65         int log_cpus = (ebx >> 16) & 0xff;
66         log_cpus = 1UL << fls(log_cpus - 1); /* round up to power of 2 */
67
68         /* Only populate the MPS tables with the first logical CPU in each
69            package */
70         if ((cpuid_features & (1 << 28)) && (i & (log_cpus - 1)) != 0)
71             continue;
72
73         actual_cpu_count++;
74
75         memset(cpu, 0, sizeof(*cpu));
76         cpu->type = MPT_TYPE_CPU;
77         cpu->apicid = i;
78         cpu->apicver = 0x11;
79         /* cpu flags: enabled, bootstrap cpu */
80         if (i < CountCPUs)
81             cpu->cpuflag = 1 | ((i == 0) ? 2 : 0);
82         else
83             cpu->cpuflag = 0;
84         if (cpuid_signature) {
85             cpu->cpusignature = cpuid_signature;
86             cpu->featureflag = cpuid_features;
87         } else {
88             cpu->cpusignature = 0x600;
89             cpu->featureflag = 0x201;
90         }
91     }
92
93     config->entrycount = actual_cpu_count + 2 + 16;
94
95     /* isa bus */
96     struct mpt_bus *bus = (void*)&cpus[actual_cpu_count];
97     memset(bus, 0, sizeof(*bus));
98     bus->type = MPT_TYPE_BUS;
99     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
100
101     /* ioapic */
102     u8 ioapic_id = CountCPUs;
103     struct mpt_ioapic *ioapic = (void*)&bus[1];
104     memset(ioapic, 0, sizeof(*ioapic));
105     ioapic->type = MPT_TYPE_IOAPIC;
106     ioapic->apicid = ioapic_id;
107     ioapic->apicver = 0x11;
108     ioapic->flags = 1; // enable
109     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
110
111     /* irqs */
112     struct mpt_intsrc *intsrc = (void*)&ioapic[1];
113     for (i = 0; i < 16; i++) {
114         memset(intsrc, 0, sizeof(*intsrc));
115         intsrc->type = MPT_TYPE_INTSRC;
116         intsrc->srcbusirq = i;
117         intsrc->dstapic = ioapic_id;
118         intsrc->dstirq = i;
119         if (qemu_cfg_irq0_override()) {
120             /* Destination 2 is covered by irq0->inti2 override (i ==
121                0). Source IRQ 2 is unused */
122             if (i == 0)
123                 intsrc->dstirq = 2;
124             else if (i == 2)
125                 intsrc--;
126         }
127         intsrc++;
128     }
129
130     // Set checksum.
131     config->length = (void*)intsrc - (void*)config;
132     config->checksum -= checksum(config, config->length);
133
134     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
135             floating, config, config->length);
136 }