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