Use MaxCountCPUs during building of per cpu 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 void
14 mptable_init(void)
15 {
16     if (! CONFIG_MPTABLE)
17         return;
18
19     dprintf(3, "init MPTable\n");
20
21     if (MaxCountCPUs <= 1)
22         // Building an mptable on uniprocessor machines confuses some OSes.
23         return;
24
25     int length = (sizeof(struct mptable_config_s)
26                   + sizeof(struct mpt_cpu) * MaxCountCPUs
27                   + sizeof(struct mpt_bus)
28                   + sizeof(struct mpt_ioapic)
29                   + sizeof(struct mpt_intsrc) * 16);
30     struct mptable_config_s *config = malloc_fseg(length);
31     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
32     if (!config || !floating) {
33         dprintf(1, "No room for MPTABLE!\n");
34         return;
35     }
36
37     /* floating pointer structure */
38     memset(floating, 0, sizeof(*floating));
39     floating->signature = MPTABLE_SIGNATURE;
40     floating->physaddr = (u32)config;
41     floating->length = 1;
42     floating->spec_rev = 4;
43     floating->checksum -= checksum(floating, sizeof(*floating));
44
45     // Config structure.
46     memset(config, 0, sizeof(*config));
47     config->signature = MPCONFIG_SIGNATURE;
48     config->spec = 4;
49     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
50     memcpy(config->productid, "0.1         ", sizeof(config->productid));
51     config->entrycount = MaxCountCPUs + 2 + 16;
52     config->lapic = BUILD_APIC_ADDR;
53
54     // CPU definitions.
55     u32 cpuid_signature, ebx, ecx, cpuid_features;
56     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
57     struct mpt_cpu *cpus = (void*)&config[1];
58     int i;
59     for (i = 0; i < MaxCountCPUs; i++) {
60         struct mpt_cpu *cpu = &cpus[i];
61         memset(cpu, 0, sizeof(*cpu));
62         cpu->type = MPT_TYPE_CPU;
63         cpu->apicid = i;
64         cpu->apicver = 0x11;
65         /* cpu flags: enabled, bootstrap cpu */
66         if (i < CountCPUs)
67             cpu->cpuflag = 1 | (i == 0) ? 2 : 0;
68         else
69             cpu->cpuflag = 0;
70         if (cpuid_signature) {
71             cpu->cpusignature = cpuid_signature;
72             cpu->featureflag = cpuid_features;
73         } else {
74             cpu->cpusignature = 0x600;
75             cpu->featureflag = 0x201;
76         }
77     }
78
79     /* isa bus */
80     struct mpt_bus *bus = (void*)&cpus[MaxCountCPUs];
81     memset(bus, 0, sizeof(*bus));
82     bus->type = MPT_TYPE_BUS;
83     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
84
85     /* ioapic */
86     u8 ioapic_id = CountCPUs;
87     struct mpt_ioapic *ioapic = (void*)&bus[1];
88     memset(ioapic, 0, sizeof(*ioapic));
89     ioapic->type = MPT_TYPE_IOAPIC;
90     ioapic->apicid = ioapic_id;
91     ioapic->apicver = 0x11;
92     ioapic->flags = 1; // enable
93     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
94
95     /* irqs */
96     struct mpt_intsrc *intsrc = (void*)&ioapic[1];
97     for (i = 0; i < 16; i++) {
98         memset(intsrc, 0, sizeof(*intsrc));
99         intsrc->type = MPT_TYPE_INTSRC;
100         intsrc->srcbusirq = i;
101         intsrc->dstapic = ioapic_id;
102         intsrc->dstirq = i;
103         if (qemu_cfg_irq0_override()) {
104             /* Destination 2 is covered by irq0->inti2 override (i ==
105                0). Source IRQ 2 is unused */
106             if (i == 0)
107                 intsrc->dstirq = 2;
108             else if (i == 2)
109                 intsrc--;
110         }
111         intsrc++;
112     }
113
114     // Set checksum.
115     config->length = (void*)intsrc - (void*)config;
116     config->checksum -= checksum(config, config->length);
117
118     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
119             floating, config, config->length);
120 }