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