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